Multiple pyqt window switches

Source: Internet
Author: User

I recently wrote a software program using pyqt. When I click a new window in the implementation menu bar, it is severely shelled. I found that wxpython's ideas and methods are completely unfeasible. There are too few Chinese materials for pyqt. After reading the English documents and QT documents, I pushed the implementation method of pyqt and finally got it done. Below is a small demo.

The code for the main interface is as follows:

#-*-Coding: UTF-8-*-from pyqt4 import qtcore, qtguifrom dialog1 import dialog1from dialog2 import dialog2import systry: _ fromutf8 = qtcore. qstring. fromutf8except attributeerror: def _ fromutf8 (s): Return stry: _ encoding = qtgui. qapplication. unicodeutf8 def _ translate (context, text, disambig): Return qtgui. qapplication. translate (context, text, disambig, _ encoding) failed t attributeerror: def _ translate (context, text, disambig): Return qtgui. qapplication. translate (context, text, disambig) Class mainwindow (qtgui. qwidget): dialogw.signal = qtcore. pyqtsignal () # defines a non-parameter signal. The serial port setting and the sub-station initialization signal dialog2_signal = qtcore. pyqtsignal () # defines a non-parameter signal. The serial port setting and the sub-station initialization signal exit_signal = qtcore. pyqtsignal () # defines a non-parameter signal. The serial port setting and the station initialization signal def _ init _ (Self): Super (mainwindow, self ). _ init _ () def setupui (self, form): form. setobjectname (_ fromutf8 ("form") form. resize (400,300) self. form = form self. pushbutton = qtgui. qpushbutton (form) self. pushbutton. setgeometry (qtcore. qrect (70, 90, 75, 23) self. pushbutton. setobjectname (_ fromutf8 ("Pushbutton") self. pushbutton_2 = qtgui. qpushbutton (form) self. pushbutton_2.setgeometry (qtcore. qrect (240, 90, 75, 23) self. pushbutton_2.setobjectname (_ fromutf8 ("pushbutton_2") self. pushbutton_3 = qtgui. qpushbutton (form) self. pushbutton_3.setgeometry (qtcore. qrect (150,160, 75, 23) self. pushbutton_3.setobjectname (_ fromutf8 ("pushbutton_3") self. label = qtgui. qlabel (form) self. label. setgeometry (qtcore. qrect (170, 40, 54, 16) self. label. setobjectname (_ fromutf8 ("label") self. retranslateui (form) qtcore. qmetaobject. connectslotsbyname (form) # connect the signal to the specified slot self. pushbutton. clicked. 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. setwindowtitle (_ 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", "quit", 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 () # You can also self. form. close () def on_pushbutton_2_clicked (Self): Self. form. close () form1 = qtgui. qdialog () UI = dialog2 () UI. setupui (form1) form1.show () form1.exec _ () self. form. show () If _ name _ = '_ main _': APP = qtgui. qapplication (sys. argv) form = qtgui. qwidget () window = mainwindow () window. setupui (form) form. show () sys.exit(app.exe C _ () Pass


The code of the dialog1 interface is as follows:

#-*-Coding: UTF-8-*-from pyqt4 import qtcore, qtguitry: _ fromutf8 = qtcore. qstring. fromutf8except attributeerror: def _ fromutf8 (s): Return stry: _ encoding = qtgui. qapplication. unicodeutf8 def _ translate (context, text, disambig): Return qtgui. qapplication. translate (context, text, disambig, _ encoding) failed t attributeerror: def _ translate (context, text, disambig): Return qtgui. qapplication. translate (context, text, disambig) Class dialog1 (qtgui. qwidget): def setupui (self, DIALOG): dialog. setobjectname (_ fromutf8 ("dialog") dialog. resize (400,300) self. form = dialog self. label = qtgui. qlabel (DIALOG) self. label. setgeometry (qtcore. qrect (180, 50, 54, 12) self. label. setobjectname (_ fromutf8 ("label") self. dialog?pushbutton = qtgui. qpushbutton (DIALOG) self. dialog1_pushbutton.setgeometry (qtcore. qrect (160,130, 75, 23) self. dialog?pushbutton.setobjectname (_ fromutf8 ("Pushbutton") self. retranslateui (DIALOG) qtcore. qmetaobject. connectslotsbyname (DIALOG) # connect the signal to the specified slot self. dialog?pushbutton.clicked.connect (self. on_dialog1_pushbutton_clicked) def retranslateui (self, DIALOG): dialog. setwindowtitle (_ translate ("dialog", "dialog", none) self. label. settext (_ translate ("dialog", "dialog1", none) self. dialog1_pushbutton.settext (_ translate ("dialog", "", none) def on_dialog?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 () sys.exit(app.exe C _())

The code of the dialog2 interface is as follows:

#-*-Coding: UTF-8-*-from pyqt4 import qtcore, qtguitry: _ fromutf8 = qtcore. qstring. fromutf8except attributeerror: def _ fromutf8 (s): Return stry: _ encoding = qtgui. qapplication. unicodeutf8 def _ translate (context, text, disambig): Return qtgui. qapplication. translate (context, text, disambig, _ encoding) failed t 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,300) self. form = dialog self. label = qtgui. qlabel (DIALOG) self. label. setgeometry (qtcore. qrect (180, 60, 54, 12) self. label. setobjectname (_ fromutf8 ("label") self. pushbutton = qtgui. qpushbutton (DIALOG) self. pushbutton. setgeometry (qtcore. qrect (160,140, 75, 23) self. pushbutton. setobjectname (_ fromutf8 ("Pushbutton") self. retranslateui (DIALOG) qtcore. qmetaobject. connectslotsbyname (DIALOG) # connect the signal to the specified slot self. pushbutton. clicked. connect (self. on_pushbutton_clicked) def retranslateui (self, DIALOG): dialog. setwindowtitle (_ translate ("dialog", "dialog", none) self. label. settext (_ translate ("dialog", "dialog2", none) self. pushbutton. settext (_ translate ("dialog", "Return main form", none) def on_pushbutton_clicked (Self): Self. form. close () If _ name _ = "_ main _": Import sys APP = qtgui. qapplication (sys. argv) dialog = qtgui. qdialog () UI = dialog2 () UI. setupui (DIALOG) dialog. show () sys.exit(app.exe C _())


Bind the button to the processing function of the new pop-up interface. The slot connection mode is as follows:

self.pushButton.clicked.connect(self.on_pushButton_clicked)


If the menu item is bound to the processing function of the new pop-up interface, the slot connection method should be used:

QtCore.QObject.connect(self.set_value_menu, QtCore.SIGNAL(_fromUtf8("triggered()")), self.open_set_value_form)

The slot processing functions are basically the same.

If the original interface is not displayed, you only need to add the original interface hide (), such:

self.form.hide()

This step is not required if the original window remains visible when a new window is displayed. In this case, to select a top-level form from the original window, use show () when displaying the new window ():

Form1.show()

If the new window is a fixed top-level form and the original form is overwritten, use exec _():

Form1.exec_()



Multiple pyqt window switches

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.