1 import sys 2 from pyqt5.qtwidgets import * 3 from pyqt5.qtcore import * 4 from pyqt5.qtgui import * 5 ##################### ########################### 6 ####### create the main window 7 ## ######################################## ###### 8 class firstmainwindow (qmainwindow): 9 def _ init _ (self, * ARGs, ** kwargs): 10 super (). _ init _ (* ARGs, ** kwargs) 11 self. setwindowtitle ('main interface ') 12 13 ###### create an interface ##### 14 self. centr Alwidget = qwidget () 15 self. setcentralwidget (self. centralwidget) 16 self. layout = qvboxlayout (self. centralwidget) 17 18 # Set the top three buttons 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 ("Open Main Interface") 25 self. buttonlayout. addwidget (self. pushbutton1) 26 27 self. pushbutton 2 = qpushbutton () 28 self. pushbutton2.settext ("Open dialog") 29 self. buttonlayout. addwidget (self. pushbutton2) 30 31 self. pushbutton3 = qpushbutton () 32 self. pushbutton3.settext ("Open prompt box") 33 self. buttonlayout. addwidget (self. pushbutton3) 34 35 # Set the intermediate text 36 self. label = qlabel () 37 self. label. settext ("first main interface") 38 self. label. setsizepolicy (qsizepolicy. expanding, qsizepolicy. expanding) 39 self. labe L. setalignment (QT. aligncenter) 40 self. label. setfont (qfont ("Roman times", 50, qfont. bold) 41 self. layout. addwidget (self. label) 42 43 # Set the status bar 44 self. statusbar (). showmessage ("current user: Mono Lion") 45 46 # window maximization 47 self. showmaximized () 48 49 ###### three button events ##### 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 # button 1: Open the main interface 55 windowlist = [] 56 def on_pushbutton1_clicked (Self): 57 the_window = secondwindow () 58 self. windowlist. append (the_window) # Note: If this sentence is absent, another main interface will not be opened! 59 self. close () 60 the_00000000show () 61 62 63 # button 2: Open the dialog box 64 def on_pushbutton2_clicked (Self): 65 the_dialog = testdemodialog () 66 If the_dialog.exec _ () = qdialog. accepted: 67 pass 68 69 # button 3: Open the prompt box 70 def on_pushbutton3_clicked (Self): 71 qmessagebox. information (self, "prompt", "this is the information box! ") 72 # qmessagebox. Question (self," prompt "," this is the question box! ") 73 # qmessagebox. Warning (self," prompt "," this is the warning box! ") 74 # qmessagebox. About (self," prompt "," this is the about box! ") 75 76 77 ##################################### ########### 78 ####### 79 ################# ############################## 80 Class secondwindow (qmainwindow): 81 def _ init _ (self, * ARGs, ** kwargs): 82 super (). _ init _ (* ARGs, ** kwargs) 83 self. setwindowtitle ('second main interface ') 84 85 # Set intermediate text 86 self. label = qlabel () 87 self. label. settext ("second main interface") 88 self. label. setsizepolicy (qsizepolicy. ex Panding, 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 # Set the status bar 94 self. statusbar (). showmessage ("current user: Mono Lion") 95 96 # maximum window 97 self. showmaximized () 98 99 100 ###### rewrite and close the event and return to the first page. 101 windowlist = [] 102 def closeevent (self, event): 103 the_window = firstmainwindow () 104 self. WL Ist. append (the_window) # Note: If this sentence is absent, another main interface is not opened! 105 the_1_1_show () 106 event. accept () 107 108 ###################################### ########## 109 ####### Dialog Box 110 #################### ########################### 111 class testdemodialog (qdialog): 112 def _ init _ (self, * ARGs, ** kwargs): 113 super (). _ init _ (* ARGs, ** kwargs) 114 self. setwindowtitle ('Dialog box ') 115 116 ### set Dialog Box Type 117 self. setwindowflags (QT. tool) 118 119 ##################################### ########### 121 ####### getting started with the program ################## ############################ 123 If _ name _ =" _ main __": 124 APP = qapplication (sys. argv) 125 the_mainwindow = firstmainwindow () 126 the_main1_show () 127 sys.exit(app.exe C _())
The main interface of pyqt5 opens the new main interface, opens the dialog, and opens the implementation template of the prompt box.