用PySid开发GUI

2024-05-11 03:21:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在一个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()

Tags: 代码fromimportselfmaindefsysgui
1条回答
网友
1楼 · 发布于 2024-05-11 03:21:49

一般来说,在GUI编程中,你必须熟悉父控件和子控件的概念。如果你想让你的按钮在你的窗口内,那么后者应该是前者的孩子。在

所以使用:

button = QPushButton("Test", parent=self)

而不是:

^{pr2}$

希望这有帮助!在

相关问题 更多 >