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实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

selenium+python截图不成功的解决方法

selenium+python,使用webdriver的截图函数get_screenshot_as_file()截图,代码如下: from selenium import webdr...

Python 函数绘图及函数图像微分与积分

Python 函数绘图及函数图像微分与积分

前言 在学校太闲,就写了这个程序,可以绘制函数图像,并且可以绘制其导函数图像和不定积分的图像,效果非常不错。 效果图 说明 1,程序无法绘制复数图像,若函数返回一个复数,将自动取模作为...

python sys,os,time模块的使用(包括时间格式的各种转换)

sys模块 sys.argv: 实现从程序外部向程序传递参数。 位置参数argv[0]代表py文件本身,运行方法 python xx.py 参数1,参数2 。。 self = s...

Python解析Excle文件中的数据方法

Python解析Excle文件中的数据方法

在公司里面,人力资源部每到发工资的时候就会头疼,如果公司内部有100多号员工,那么发完工资后需要给员工发送工资条的话,那么就需要截图如下图, 但是在公司的薪水保密协议不允许公开所有人的...

使用Python中的tkinter模块作图的方法

使用Python中的tkinter模块作图的方法

python简述: Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。自从20世纪90年代初Python语言诞生至今,它逐渐被广泛应用于处理系统管理任务和Web编程。P...