The PyQt5 Main Window dynamically loads the Widget instance code, pyqt5widget
This article focuses on the sample code for dynamically loading widgets in the main PyQt5 window, as shown below.
We use Qt Designer to design two windows named MainForm and ChildrenForm ). We add a grid layout in the blank center of the Main Window and name it MaingridLayout. ChildrenForm needs to be put in.
Write code
From PyQt5 import QtWidgets from MainForm import Ui_MainForm from Children import Ui_Form from PyQt5.QtWidgets import QFileDialog class MainForm (QtWidgets. QMainWindow, Ui_MainForm): def _ init _ (self): super (MainForm, self ). _ init _ () self. setupUi (self) self. child = ChildrenForm () # self. child = children () generates the subwindow instance self. child self. fileOpen. triggered. connect (self. openMsg) # The Menu click event is triggered self. fileClose. triggered. connect (self. close) self. actionTst. triggered. connect (self. childShow) # click actionTst. The child window is displayed in the MaingridLayout of the main window. def childShow (self): self. maingridLayout. addWidget (self. child) # Add subwindow self. child. show () def openMsg (self): file, OK = QFileDialog. getOpenFileName (self, "open", "C:/", "All Files (*); Text Files (*. txt) ") self. statusbar. showMessage (file) # display the file address class ChildrenForm (QtWidgets. QWidget, Ui_Form): def _ init _ (self): super (ChildrenForm, self ). _ init _ () self. setupUi (self) if _ name __= = "_ main _": import sys app = QtWidgets. QApplication (sys. argv) myshow = MainForm () myshow. show () sys.exit(app.exe c _())
Summary
The above is all the content about dynamically loading the Widget instance code in the main PyQt5 window. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!