pyqt5 主界面打开新主界面、打开Dialog、打开提示框的实现模板
1 import sys 2 from PyQt5.QtWidgets import * 3 from PyQt5.QtCore import * 4 from PyQt5.QtGui import * 5 ################################################ 6 #######创建主窗口 7 ################################################ 8 class FirstMainWindow(QMainWindow): 9 def __init__(self, *args, **kwargs): 10 super().__init__(*args, **kwargs) 11 self.setWindowTitle('主界面') 12 13 ###### 创建界面 ###### 14 self.centralwidget = QWidget() 15 self.setCentralWidget(self.centralwidget) 16 self.Layout = QVBoxLayout(self.centralwidget) 17 18 # 设置顶部三个按钮 19 self.topwidget = QWidget() 20 self.Layout.addWidget(self.topwidget) 21 self.buttonLayout = QHBoxLayout(self.topwidget) 22 23 self.pushButton1 = QPushButton() 24 self.pushButton1.setText("打开主界面") 25 self.buttonLayout.addWidget(self.pushButton1) 26 27 self.pushButton2 = QPushButton() 28 self.pushButton2.setText("打开对话框") 29 self.buttonLayout.addWidget(self.pushButton2) 30 31 self.pushButton3 = QPushButton() 32 self.pushButton3.setText("打开提示框") 33 self.buttonLayout.addWidget(self.pushButton3) 34 35 # 设置中间文本 36 self.label = QLabel() 37 self.label.setText("第一个主界面") 38 self.label.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)) 39 self.label.setAlignment(Qt.AlignCenter) 40 self.label.setFont(QFont("Roman times", 50, QFont.Bold)) 41 self.Layout.addWidget(self.label) 42 43 # 设置状态栏 44 self.statusBar().showMessage("当前用户:一心狮") 45 46 # 窗口最大化 47 self.showMaximized() 48 49 ###### 三个按钮事件 ###### 50 self.pushButton1.clicked.connect(self.on_pushButton1_clicked) 51 self.pushButton2.clicked.connect(self.on_pushButton2_clicked) 52 self.pushButton3.clicked.connect(self.on_pushButton3_clicked) 53 54 # 按钮一:打开主界面 55 windowList = [] 56 def on_pushButton1_clicked(self): 57 the_window =SecondWindow() 58 self.windowList.append(the_window) ##注:没有这句,是不打开另一个主界面的! 59 self.close() 60 the_window.show() 61 62 63 # 按钮二:打开对话框 64 def on_pushButton2_clicked(self): 65 the_dialog = TestdemoDialog() 66 if the_dialog.exec_() == QDialog.Accepted: 67 pass 68 69 # 按钮三:打开提示框 70 def on_pushButton3_clicked(self): 71 QMessageBox.information(self, "提示", "这是information框!") 72 #QMessageBox.question(self, "提示", "这是question框!") 73 #QMessageBox.warning(self, "提示", "这是warning框!") 74 #QMessageBox.about(self, "提示", "这是about框!") 75 76 77 ################################################ 78 #######第二个主界面 79 ################################################ 80 class SecondWindow(QMainWindow): 81 def __init__(self, *args, **kwargs): 82 super().__init__(*args, **kwargs) 83 self.setWindowTitle('第二主界面') 84 85 # 设置中间文本 86 self.label = QLabel() 87 self.label.setText("第二个主界面") 88 self.label.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)) 89 self.label.setAlignment(Qt.AlignCenter) 90 self.label.setFont(QFont("Roman times", 50, QFont.Bold)) 91 self.setCentralWidget(self.label) 92 93 # 设置状态栏 94 self.statusBar().showMessage("当前用户:一心狮") 95 96 # 窗口最大化 97 self.showMaximized() 98 99 100 ###### 重写关闭事件,回到第一界面 101 windowList = [] 102 def closeEvent(self, event): 103 the_window = FirstMainWindow() 104 self.windowList.append(the_window) ##注:没有这句,是不打开另一个主界面的! 105 the_window.show() 106 event.accept() 107 108 ################################################ 109 #######对话框 110 ################################################ 111 class TestdemoDialog(QDialog): 112 def __init__(self, *args, **kwargs): 113 super().__init__(*args, **kwargs) 114 self.setWindowTitle('对话框') 115 116 ### 设置对话框类型 117 self.setWindowFlags(Qt.Tool) 118 119 120 ################################################ 121 #######程序入门 122 ################################################ 123 if __name__ == "__main__": 124 app = QApplication(sys.argv) 125 the_mainwindow = FirstMainWindow() 126 the_mainwindow.show() 127 sys.exit(app.exec_())