PYQT for multi-window switching

Source: Internet
Author: User
This article is mainly for you to introduce the PYQT implementation of multi-window switching method, with a certain reference value, interested in small partners can refer to

Recently made a software, write with PYQT, in the implementation of the menu bar click Pop-up new window when seriously stuck, found that with Wxpython thought and way to do completely impossible to achieve. PYQT's Chinese information is too small. Read a bit of information in English and QT data, reverse push PYQT implementation method, finally fix. Here is a small demo.

The code for the main interface is as follows:



#-*-Coding:utf-8-*-from PyQt4 import Qtcore, Qtgui from dialog1 import Dialog1 from dialog2 import Dialog2 import sy s try: _fromutf8 = QtCore.QString.fromUtf8 except Attributeerror:def _fromutf8 (s): return s try: _encoding =  QtGui.QApplication.UnicodeUTF8 def _translate (context, text, disambig): Return QtGui.QApplication.translate (Context, Text, disambig, _encoding) except Attributeerror:def _translate (context, text, disambig): Return Qtgui.qapplicatio N.translate (context, text, disambig) class MainWindow (qtgui.qwidget): Dialog1_signal = qtcore.pyqtsignal () #定 A signal without parameters, serial port settings and sub-station initialization signal dialog2_signal = qtcore.pyqtsignal () #定义一个无参数的信号, serial port settings and sub-station initialization signal exit_signal = Qtcore.pyqtsign Al () #定义一个无参数的信号, serial port settings and sub-station initialization signal def __init__ (self): Super (Mainwindow,self). __init__ () def setupui (self, form): Form.setobjectname (_fromutf8 ("form")) Form.resize (+) Self.form = Form Self.pushbutton = Qtgu     I.qpushbutton (Form)Self.pushButton.setGeometry (Qtcore.qrect (S, S, ()) Self.pushButton.setObjectName (_fromutf8 ("pushbutton")) Elf.pushbutton_2 = Qtgui.qpushbutton (Form) self.pushButton_2.setGeometry (Qtcore.qrect (+,-) SELF.PUSHB Utton_2.setobjectname (_fromutf8 ("pushbutton_2")) Self.pushbutton_3 = Qtgui.qpushbutton (Form) self.pushButton_3.set Geometry (Qtcore.qrect, Max, Max) Self.pushButton_3.setObjectName (_fromutf8 ("Pushbutton_3")) Self.label = Q Tgui.qlabel (Form) self.label.setGeometry (Qtcore.qrect (+,-) Self.label.setObjectName (_fromutf8 ("label") )) Self.retranslateui (form) QtCore.QMetaObject.connectSlotsByName (form) #信号连接到指定槽 Self.pushButton.clicke     D.connect (self.on_pushbutton_clicked) self.pushButton_2.clicked.connect (self.on_pushbutton_2_clicked) Self.pushButton_3.clicked.connect (self.on_pushbutton_3_clicked) def retranslateui (self, Form): Form.setwin Dowtitle (_translate ("form", "form",None)) Self.pushButton.setText (_translate ("form", "Enter Dialog1", None)) Self.pushButton_2.setText (_translate ("form", "Enter Dialog2", none)) Self.pushButton_3.setText (_translate ("form", "exit", none)) Self.label.setText (_translate ("form "," main form ", None)) def on_pushbutton_clicked (self): self.form.hide () Form1 = Qtgui.qdialog () UI = Dialog1 () ui.setupui (Form1) form1.show () form1.exec_ () self.form.show () def on_pushbutton_3_clicked (self, form ): #QtCore. Qobject.connect (Self.pushbutton_3, Qtcore.signal ("clicked ()"), Self, Qtcore.slot (Quit ())) #也可以这样 s Elf.form.close () def on_pushbutton_2_clicked (self): self.form.close () Form1 = Qtgui.qdialog () UI = Dial OG2 () ui.setupui (Form1) form1.show () form1.exec_ () self.form.show () if __name__ = = ' __main__ ': app = Qt Gui.qapplication (sys.argv) Form = qtgui.qwidget () window = MainWindow () window.setupui (Form) form.show () sys.       Exit (APP.EXEC_ ())Pass 


The code for the Dialog1 interface is as follows:


#-*-Coding:utf-8-*-from PyQt4 import Qtcore, Qtgui try: _fromutf8 = QtCore.QString.fromUtf8 except Attributeerro R:def _fromutf8 (s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate (context, text, dis AMBIG): Return QtGui.QApplication.translate (context, text, disambig, _encoding) except Attributeerror:def _translat E (context, text, disambig): Return QtGui.QApplication.translate (context, text, disambig) class Dialog1 (Qtgui.qwidget ): Def setupui (self, Dialog): Dialog.setobjectname (_fromutf8 ("Dialog")) Dialog.resize (+) Self.form = Dialog Self.label = Qtgui.qlabel (Dialog) self.label.setGeometry (Qtcore.qrect (), Self.label.setO Bjectname (_fromutf8 ("label")) Self.dialog1_pushbutton = Qtgui.qpushbutton (Dialog) self.dialog1_pushButton.setGeome Try (Qtcore.qrect, Self.dialog1_pushButton.setObjectName, ()) (_fromutf8 ("pushbutton")) Self.retransla Teui (Dialog) Qtcore.Qmetaobject.connectslotsbyname (Dialog) #信号连接到指定槽 Self.dialog1_pushButton.clicked.connect (self.on_dialog1_pushbut ton_clicked) def retranslateui (self, Dialog): Dialog.setwindowtitle (_translate ("Dialog", "Dialog", None)) s Elf.label.setText (_translate ("Dialog", "Dialog1", None)) Self.dialog1_pushButton.setText (_translate ("Dialog", "   Return main form ", None)) def on_dialog1_pushbutton_clicked (self): Self.form.close () if __name__ = =" __main__ ": Import sys App = Qtgui.qapplication (sys.argv) Dialog = Qtgui.qdialog () UI = Dialog1 () ui.setupui (Dialog) dialog.show () sy The code for the S.exit (App.exec_ ()) DIALOG2 interface is as follows: [Python] View plain copy#-*-coding:utf-8-*-from PyQt4 import Qtcore, Qtgui t Ry: _fromutf8 = QtCore.QString.fromUtf8 except Attributeerror:def _fromutf8 (s): return s try: _encoding = Qtgu I.qapplication.unicodeutf8 def _translate (context, text, disambig): Return QtGui.QApplication.translate (Context, Tex T, disambig, _encoding) except ATtributeerror:def _translate (context, text, disambig): Return QtGui.QApplication.translate (context, text, disambig) Class Dialog2 (object): Def setupui (self, Dialog): Dialog.setobjectname (_fromutf8 ("Dialog")) Dialog.resize (400 , Self.form = Dialog Self.label = Qtgui.qlabel (Dialog) self.label.setGeometry (Qtcore.qrect (180, 60, 54, 1 2)) Self.label.setObjectName (_fromutf8 ("label")) Self.pushbutton = Qtgui.qpushbutton (Dialog) self.pushbutton.s Etgeometry (Qtcore.qrect (up, up, up)) Self.pushButton.setObjectName (_fromutf8 ("pushbutton")) Self.retranslat EUi (Dialog) QtCore.QMetaObject.connectSlotsByName (Dialog) #信号连接到指定槽 self.pushButton.clicked.connect (self. on_pushbutton_clicked) def retranslateui (self, Dialog): Dialog.setwindowtitle (_translate ("Dialog", "Dialog", No NE)) self.label.setText (_translate ("Dialog", "dialog2", None)) Self.pushButton.setText (_translate ("Dialog", "Return to main form ", None)) def On_Pushbutton_clicked (self): Self.form. Close () if __name__ = = "__main__": Import sys app = Qtgui.qapplication (sys.a RGV) Dialog = Qtgui.qdialog () UI = Dialog2 () ui.setupui (Dialog) dialog.show () Sys.exit (App.exec_ ())


The button is bound to the new popup interface's handler function, using the slot connection as follows:



Self.pushButton.clicked.connect (self.on_pushbutton_clicked)


If the menu item is bound to a handler function for the new popup interface, the slot you should use is connected as follows:


QtCore.QObject.connect (Self.set_value_menu, Qtcore.signal (_fromutf8 ("triggered ()"), Self.open_set_value_form)


The slot processing functions used are basically the same.
If the original interface is not displayed, simply hide the original interface (), such as:


Self.form.hide ()


You do not need this step if you need to keep the original window visible while the new window pops up. In this case, if you want the original window to be selected as the top-level form, you should use Show () When a new window is displayed:


Form1.show ()


If the new window is a fixed top-level form and the original form is obscured, you should use EXEC_ ():


FORM1.EXEC_ ()


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.