PyQT implements multi-window switching
This article mainly introduces the method of PyQT to implement multi-window switching in detail. It has certain reference value. Interested friends can refer to
I recently made a software and wrote it in PyQT. , I was seriously stuck when I realized that a new window would pop up when I clicked on the menu bar. I found that it was completely impossible to do it with WxPython's ideas and methods. There is really too little Chinese information on PyQT. After reading some English information and QT information, I deduced the implementation method of PyQT and finally got it. Below is a small demo.
The code of the main interface is as follows:
# -*- coding: utf-8 -*- from PyQt4 import QtCore, QtGui from dialog1 import Dialog1 from dialog2 import Dialog2 import sys 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.QApplication.translate(context, text, disambig) class MainWindow(QtGui.QWidget): dialog1_signal = QtCore.pyqtSignal() #定义一个无参数的信号,串口设定与子站初始化信号 dialog2_signal = QtCore.pyqtSignal() #定义一个无参数的信号,串口设定与子站初始化信号 exit_signal = QtCore.pyqtSignal() #定义一个无参数的信号,串口设定与子站初始化信号 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) #信号连接到指定槽 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", "进入dialog1", None)) self.pushButton_2.setText(_translate("Form", "进入dialog2", None)) self.pushButton_3.setText(_translate("Form", "退出", None)) self.label.setText(_translate("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())) #也可以这样 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.exec_()) pass
Dialog1 interface The code is as follows:
# -*- coding: utf-8 -*- from PyQt4 import QtCore, QtGui 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.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.dialog1_pushButton = QtGui.QPushButton(Dialog) self.dialog1_pushButton.setGeometry(QtCore.QRect(160, 130, 75, 23)) self.dialog1_pushButton.setObjectName(_fromUtf8("pushButton")) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) #信号连接到指定槽 self.dialog1_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_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() sys.exit(app.exec_()) Dialog2界面的代码如下: [python] view plain copy # -*- coding: utf-8 -*- from PyQt4 import QtCore, QtGui 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.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) #信号连接到指定槽 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", "返回主窗体", 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.exec_())
The button is bound to the processing function of the new pop-up interface, and the slot connection method used is:
self.pushButton.clicked.connect(self.on_pushButton_clicked)
If the Menu item is bound to the handler function of the new pop-up interface, the slot should be used The connection method is:
QtCore.QObject.connect(self.set_value_menu, QtCore.SIGNAL(_fromUtf8("triggered()")), self.open_set_value_form)
The slot processing functions used by the two are basically the same.
If the original interface is not displayed, just hide() the original interface, such as:
self.form.hide()
If you need to When a new window pops up while the original window remains visible, this step is not required. And in this case, if you want the original window to be selected as the top-level form, you should use show() when displaying the new window:
Form1.show()
If the new window is a fixed top-level form and the original form is covered, exec_() should be used:
Form1.exec_()
Related recommendations:
PyQt implements interface flip switching effect
The above is the detailed content of PyQT implements multi-window switching. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Xiaomi 14Ultra is one of the most popular Xiaomi models this year. Xiaomi 14Ultra not only upgrades the processor and various configurations, but also brings many new functional applications to users. This can be seen from the sales of Xiaomi 14Ultra mobile phones. It is very popular, but there are some commonly used functions that you may not know yet. So how does Xiaomi 14Ultra switch between 4g and 5g? Let me introduce the specific content to you below! How to switch between 4g and 5g on Xiaomi 14Ultra? 1. Open the settings menu of your phone. 2. Find and select the "Network" and "Mobile Network" options in the settings menu. 3. In the mobile network settings, you will see the "Preferred network type" option. 4. Click or select this option and you will see

How to convert Win11 Home Edition to Win11 Professional Edition? In Win11 system, it is divided into Home Edition, Professional Edition, Enterprise Edition, etc., and most Win11 notebooks are pre-installed with Win11 Home Edition system. Today, the editor will show you the steps to switch from win11 home version to professional version! 1. First, right-click on this computer on the win11 desktop and properties. 2. Click Change Product Key or Upgrade Windows. 3. Then click Change Product Key after entering. 4. Enter the activation key: 8G7XN-V7YWC-W8RPC-V73KB-YWRDB and select Next. 5. Then it will prompt success, so you can upgrade win11 home version to win11 professional version.

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

In the application of excel software, we are accustomed to using shortcut keys to make some operations easier and faster. Sometimes there is related data between multiple tables in excel. When we view it, we have to constantly switch between tasks. If there is a faster switching method, it will save a lot of wasted time on switching, which will greatly help improve work efficiency. What method can be used to complete quick switching? To address this issue, the editor will talk about it today The content is: How to use the shortcut keys for switching workbooks in Excel. 1. First, you can see multiple workbooks at the bottom of the open excel table. You need to quickly switch between different workbooks, as shown in the figure below. 2. Then press the Ctrl key on the keyboard without moving, and select the job to the right if you need to

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these
