简体   繁体   English

使用PySide开发GUI

[英]Developing a GUI with PySide

I am trying to get buttons and a menu bar inside a GUI application. 我正在尝试在GUI应用程序中获取按钮和菜单栏。 When I run my code the GUI is seen with the menu bar but the button is not seen. 当我运行代码时,可以通过菜单栏看到GUI,但是看不到按钮。 Here is my sample code. 这是我的示例代码。 And the code compiles without any error. 并且代码编译没有任何错误。

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()

Generally, in GUI programming you'll have to be faimliar with the concept of parent and child widgets. 通常,在GUI编程中,您必须熟悉父级和子级小部件的概念。 If you want your button to be inside your window, then the later should be a child of the former. 如果您希望按钮位于窗口内,则后者应该是前者的子级。

So use: 因此使用:

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

Instead of: 代替:

button = QPushButton("Test")

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM