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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM