PyQt5主窗口动态加载Widget实例代码

yipeiwu_com5年前Python基础

本文研究的主要是PyQt5主窗口动态加载Widget的代码示例,具体如下。

我们通过Qt Designer设计两个窗口,命名为主窗口(MainForm)和子窗口(ChildrenForm)。我们在主窗口的空白中央添加一个栅格布局并命名为MaingridLayout,等会需要将ChildrenForm放进去。

编写代码

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()生成子窗口实例self.child 
 
 
    self.fileOpen.triggered.connect(self.openMsg)   #菜单的点击事件是triggered 
    self.fileClose.triggered.connect(self.close) 
    self.actionTst.triggered.connect(self.childShow)  #点击actionTst,子窗口就会显示在主窗口的MaingridLayout中 
 
  def childShow(self): 
    self.MaingridLayout.addWidget(self.child)     #添加子窗口 
    self.child.show() 
 
 
  def openMsg(self): 
    file,ok=QFileDialog.getOpenFileName(self,"打开","C:/","All Files (*);;Text Files (*.txt)") 
    self.statusbar.showMessage(file)          #在状态栏显示文件地址 
 
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.exec_()) 

总结

以上就是本文关于PyQt5主窗口动态加载Widget实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

Python加载带有注释的Json文件实例

由于json文件不支持注释,所以如果在json文件中标记了注释,则使用python中的json.dump()无法加载该json文件。 本文旨在解决当定义“//”为json注释时,如何正确...

解决django同步数据库的时候app models表没有成功创建的问题

问题描述: 在django中创建了一个app,而且在app中自定义创建了几个数据表,在同步的时候系统自带的表可以成功,但是models中的没有生效,而且进入对应app下的migratio...

python贪吃蛇游戏代码

python贪吃蛇游戏代码

本文实例为大家分享了python贪吃蛇游戏的具体代码,供大家参考,具体内容如下 贪吃蛇游戏截图: 首先安装pygame,可以使用pip安装pygame: pip install py...

分析Python中设计模式之Decorator装饰器模式的要点

先给出一个四人团对Decorator mode的定义:动态地给一个对象添加一些额外的职责。 再来说说这个模式的好处:认证,权限检查,记日志,检查参数,加锁,等等等等,这些功能和系统业务无...

python实现下载指定网址所有图片的方法

本文实例讲述了python实现下载指定网址所有图片的方法。分享给大家供大家参考。具体实现方法如下: #coding=utf-8 #download pictures of the u...