The PyQt5 Main Window dynamically loads the Widget instance code, pyqt5widget

Source: Internet
Author: User
Tags qt designer

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!

Related Article

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.