# Only needed for access to command line arguments import sys from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtGui import * # 自定义窗口,继承 QMainWindow class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle("My Awesome App") # 标题 label = QLabel("THIS IS AWESOME!!!") label.setAlignment(Qt.AlignCenter) self.setCentralWidget(label) toolbar = QToolBar("My main toolbar") toolbar.setIconSize(QSize(16, 16)) self.addToolBar(toolbar) button_action = QAction(QIcon("icons/bug.png"), "&Your button", self) button_action.setStatusTip("This is your button") button_action.triggered.connect(self.onMyToolBarButtonClick) button_action.setCheckable(True) button_action.setShortcut(QKeySequence("Ctrl+p")) toolbar.addAction(button_action) toolbar.addSeparator() button_action2 = QAction(QIcon("icons/bug.png"), "Your &button2", self) button_action2.setStatusTip("This your button2") button_action2.triggered.connect(self.onMyToolBarButtonClick) button_action2.setCheckable(True) toolbar.addAction(button_action2) toolbar.addSeparator() toolbar.addWidget(QLabel("Hello")) toolbar.addWidget(QCheckBox()) # 状态栏 self.setStatusBar(QStatusBar(self)) # 菜单 menu = self.menuBar() file_menu = menu.addMenu("&File") file_menu.addAction(button_action) file_menu.addSeparator() file_submenu = file_menu.addMenu("Submenu") file_submenu.addAction(button_action2) def onMyToolBarButtonClick(self, s): print("click", s) dlg = CustomDialog(self) if dlg.exec_(): print("Success!") else: print("Cancel!") class CustomDialog(QDialog): def __init__(self, *args, **kwargs): super(CustomDialog, self).__init__(*args, **kwargs) self.setWindowTitle("HELLO!") q_btn = QDialogButtonBox.Ok | QDialogButtonBox.Cancel self.buttonBox = QDialogButtonBox(q_btn) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) self.layout = QHBoxLayout() self.layout.addWidget(self.buttonBox) self.setLayout(self.layout) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() # 默认下 windows 是隐藏的 # 开启事件循环 app.exec_()