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

yipeiwu_com6年前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实现的自动发送消息功能详解

python实现的自动发送消息功能详解

本文实例讲述了python实现的自动发送消息功能。分享给大家供大家参考,具体如下: 一个简单的脚本 #-*- coding:utf-8 -*- from __future__ imp...

python从zip中删除指定后缀文件(推荐)

python从zip中删除指定后缀文件(推荐)

一,说明 环境:python2 用到的模块 os zipfile shutil 程序功能:从zip中删除指定后缀的文件,然后再自动压缩 函数说明: DelFileInZip(path,s...

Python实现自动添加脚本头信息的示例代码

前言 每个人写脚本时的格式都会有所不同,有的会注明脚本本身的一些信息,有的则开门见山,这在小团队里其实没什么,基本别人做什么你也都知道,但如果放到大的团队就比较麻烦了,因为随着人数的增多...

python中pygame模块用法实例

python中pygame模块用法实例

本文实例讲述了python中pygame模块用法,分享给大家供大家参考。具体方法如下: import pygame, sys from pygame.locals import *...

利用Python+Java调用Shell脚本时的死锁陷阱详解

前言 最近有一项需求,要定时判断任务执行条件是否满足并触发 Spark 任务,平时编写 Spark 任务时都是封装为一个 Jar 包,然后采用 Shell 脚本形式传入所需参数执行,考虑...