基本的应用程序

以下示例显示了一个基本的主 GUI 窗口,其中包含标签窗口小部件,工具栏和使用 PyQt4 的状态栏。

StackOverflow 文档

import sys
from PyQt4 import QtGui

class App(QtGui.QApplication):
    def __init__(self, sys_argv):
        super(App, self).__init__(sys_argv)
        self.build_ui()

    def build_ui(self):
        # build a main GUI window
        self.main_window = QtGui.QMainWindow()
        self.main_window.setWindowTitle('App')
        self.main_window.show()

        # add a label to the main window
        label = QtGui.QLabel('Label')
        self.main_window.setCentralWidget(label)

        # add a toolbar with an action button to the main window
        action = QtGui.QAction('Toolbar action', self)
        toolbar = QtGui.QToolBar()
        toolbar.addAction(action)
        self.main_window.addToolBar(toolbar)

        # add a status bar to the main window
        status_bar = QtGui.QStatusBar()
        status_bar.showMessage('Status bar')
        self.main_window.setStatusBar(status_bar)

if __name__ == '__main__':
    app = App(sys.argv)
    sys.exit(app.exec_())