我试图在一个GUI应用程序中获得按钮和菜单栏。当我运行我的代码时,可以看到菜单栏上的GUI,但看不到按钮。这是我的示例代码。代码编译时没有任何错误。在
import sys
from PySide.QtGui import *
from PySide.QtCore import *
class guiwindow(QMainWindow):
def __init__(self):
super(guiwindow,self).__init__()
self.menubar()
def menubar(self):
textEdit = QWidget()
self.setCentralWidget(textEdit)
exitAction = QAction('Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(self.close)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAction)
self.setGeometry(400, 100, 1200, 800)
self.setWindowTitle("Menubar + Buttons")
button = QPushButton("Test")
hbox = QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(button)
self.setLayout(hbox)
self.show()
def main():
app = QApplication(sys.argv)
ex = guiwindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
一般来说,在GUI编程中,你必须熟悉父控件和子控件的概念。如果你想让你的按钮在你的窗口内,那么后者应该是前者的孩子。在
所以使用:
而不是:
^{pr2}$希望这有帮助!在
相关问题 更多 >
编程相关推荐