Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to display multiple classes in a single interface with Pyqt5

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how Pyqt5 combines multiple classes in one interface. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Background:

When you do complex programs, it is impossible to write all the UI in one python file, so we need to assemble multiple UI files and their corresponding logic in different classes, and then combine them in a UI interface.

Examples are as follows:

1. The original main interface to be combined is mainly controlled by QTabWidget, which displays the layouts in different classes to the corresponding tab.

#-*-coding: utf-8-*-import sysfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtWidgets import QApplication class Ui_MainWindow (QtWidgets.QWidget): def _ _ init__ (self): super (Ui_MainWindow,self). _ _ init__ () self.setupUi () def setupUi (self): self.allLayoutV = QtWidgets.QVBoxLayout () self.allLayoutV.setContentsMargins (0,0,0 0) self.allLayoutV.setObjectName ("allLayoutV") self.tabWidget = QtWidgets.QTabWidget () self.tabWidget.setObjectName ("tabWidget") self.tab = QtWidgets.QWidget () self.tab.setObjectName ("tab") self.tabWidget.addTab (self.tab "") self.tab_2 = QtWidgets.QWidget () self.tab_2.setObjectName ("tab_2") self.tabWidget.addTab (self.tab_2, ") self.tab_3 = QtWidgets.QWidget () self.tab_3.setObjectName (" tab_3 ") self.tabWidget.addTab (self.tab_3 ") self.allLayoutV.addWidget (self.tabWidget) self.retranslateUi () self.tabWidget.setCurrentIndex (0) self.setLayout (self.allLayoutV) self.show () def retranslateUi (self): _ translate = QtCore.QCoreApplication.translate self.tabWidget.setTabText (self.tabWidget.indexOf (self.tab), _ translate (" MainWindow ") "Tab 1") self.tabWidget.setTabText (self.tabWidget.indexOf (self.tab_2), _ translate ("MainWindow", "Tab 2")) self.tabWidget.setTabText (self.tabWidget.indexOf (self.tab_3), _ translate ("MainWindow", "Tab 3")) if _ _ name__ ='_ main__': app = QApplication (sys.argv) ex = Ui_MainWindow () app.exit (app.exec_ ())

Interface 1

Class Test (QtWidgets.QWidget): def _ init__ (self, parent=None): super (Test, self). _ init__ (parent) # for testing lay = QtWidgets.QVBoxLayout (self) lay.addWidget (QtWidgets.QPushButton ("Test"))

Interface 2

Class Train_Haar (QtWidgets.QWidget): def _ init__ (self, parent=None): super (Train_Haar, self). _ init__ (parent) # for testing lay = QtWidgets.QVBoxLayout (self) lay.addWidget (QtWidgets.QPushButton ("Train_Haar"))

Interface 3

Class Train_HOG (QtWidgets.QWidget): def _ init__ (self, parent=None): super (Train_HOG, self). _ _ init__ (parent) # for testing lay = QtWidgets.QVBoxLayout (self) lay.addWidget (QtWidgets.QPushButton ("Train_HOG")) Composite Class final Code #-*-coding: utf-8-*-import sysfrom PyQt5 import QtCore, QtGui QtWidgetsfrom PyQt5.QtWidgets import QApplication# interface 1class Test (QtWidgets.QWidget): def _ init__ (self, parent=None): super (Test, self). _ init__ (parent) # for testing lay = QtWidgets.QVBoxLayout (self) lay.addWidget (QtWidgets.QPushButton ("Test")) # interface 2class Train_Haar (QtWidgets.QWidget): def _ init__ (self, parent=None): super (Train_Haar) Self). _ init__ (parent) # for testing lay = QtWidgets.QVBoxLayout (self) lay.addWidget (QtWidgets.QPushButton ("Train_Haar")) # Interface 3class Train_HOG (QtWidgets.QWidget): def _ init__ (self, parent=None): super (Train_HOG Self). _ init__ (parent) # for testing lay = QtWidgets.QVBoxLayout (self) lay.addWidget (QtWidgets.QPushButton ("Train_HOG")) class Ui_MainWindow (QtWidgets.QWidget): def _ init__ (self): super (Ui_MainWindow,self). _ init__ () self.setupUi () def setupUi (self): self.allLayoutV = QtWidgets.QVBoxLayout () self.allLayoutV.setContentsMargins (0) 0,0,0) self.allLayoutV.setObjectName ("allLayoutV") self.tabWidget = QtWidgets.QTabWidget () self.tabWidget.setObjectName ("tabWidget") # tab1 example-self.tab = QtWidgets.QWidget () self.tab.setObjectName ("tab") # create Test class object test = Test () # create a temporary layout Used to store the layout obtained from Test and its logic lay = QtWidgets.QGridLayout () # get the layout and its logic in the Test class. Note that the addWeidget method must be used here. If you assign the layout directly, self.tab.setLayout (test.layout ()) can only get the layout in Test, but the control logic written in Test will not be called. Because directly assigned, you only get the layout''lay.addWidget (test) # set the temporary layout to tab self.tab.setLayout (lay) self.tabWidget.addTab (self.tab) ") # tab2 example 2 self.tab_2 = QtWidgets.QWidget () self.tab_2.setObjectName (" tab_2 ") train_Haar = Train_Haar () lay = QtWidgets.QGridLayout () lay.addWidget (train_Haar) self.tab_2.setLayout (lay) self.tabWidget.addTab (self.tab_2 ") # tab3 example 3 self.tab_3 = QtWidgets.QWidget () self.tab_3.setObjectName (" tab_3 ") train_Hog = Train_HOG () lay = QtWidgets.QGridLayout () lay.addWidget (train_Hog) self.tab_3.setLayout (lay) self.tabWidget.addTab (self.tab_3 ") self.allLayoutV.addWidget (self.tabWidget) self.retranslateUi () self.tabWidget.setCurrentIndex (0) self.setLayout (self.allLayoutV) self.show () def retranslateUi (self): _ translate = QtCore.QCoreApplication.translate self.tabWidget.setTabText (self.tabWidget.indexOf (self.tab), _ translate (" MainWindow ") "Tab 1") self.tabWidget.setTabText (self.tabWidget.indexOf (self.tab_2), _ translate ("MainWindow", "Tab 2")) self.tabWidget.setTabText (self.tabWidget.indexOf (self.tab_3), _ translate ("MainWindow") "Tab 3") if _ _ name__ = ='_ main__': app = QApplication (sys.argv) ex = Ui_MainWindow () app.exit (app.exec_ ()) final effect:

Summary:

Here I use QTabWidget, in fact, you can use any layout, the key code is addWidget, so you can use QGridLayout and other layout containers.

# tab1 self.tab = QtWidgets.QWidget () self.tab.setObjectName ("tab") # create Test class object test = Test () # create a temporary layout for storing the layout obtained from Test and its logic lay = QtWidgets.QGridLayout () # get the layout in the Test class and its logic''Note that the addWeidget method must be used here If you assign a layout directly, self.tab.setLayout (test.layout ()) can only get the layout in Test, but the control logic written in Test will not be called. Because directly assigning you only get the layout''lay.addWidget (test) # set the temporary layout to tab self.tab.setLayout (lay), this article on "how Pyqt5 combines multiple classes in one interface" is shared here. I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good. Please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report