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

作者:翻滚吧挨踢男 时间:2023-06-04 21:20:32 

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

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

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

编写代码


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_())

来源:http://blog.csdn.net/a359680405/article/details/45172155

标签:python,pyqt,widget
0
投稿

猜你喜欢

  • 一小时学会TensorFlow2之自定义层

    2021-12-22 18:00:11
  • 数据库自动化技术弥补数据库DBA短缺难题

    2009-02-04 16:53:00
  • js/jq仿window文件夹框选操作插件

    2024-04-16 09:50:23
  • Python随机生成8位密码的示例详解

    2023-09-15 21:04:03
  • 使用Python的PIL模块来进行图片对比

    2022-04-28 19:18:36
  • python GUI库图形界面开发之PyQt5信号与槽的高级使用技巧装饰器信号与槽详细使用方法与实例

    2022-11-27 15:06:22
  • MySQL延时复制库方法详解

    2024-01-26 14:21:51
  • IE6/7/8/9中Table/Select的innerHTML不能赋值的解决方法

    2024-04-10 16:16:48
  • Django 根据数据模型models创建数据表的实例

    2021-08-25 18:13:31
  • 在Mac OS系统上安装Python的Pillow库的教程

    2021-09-29 15:03:26
  • 原生js写的放大镜效果

    2024-04-30 08:51:13
  • SQL语句练习实例之一——找出最近的两次晋升日期与工资额

    2011-10-24 20:16:42
  • python中各种路径设置的方法详解

    2022-12-09 23:58:09
  • anaconda升级sklearn版本的实现方法

    2021-08-07 02:28:18
  • ORCLE 表中列的修改

    2009-07-28 10:42:00
  • 利用d3.js实现蜂巢图表带动画效果

    2024-04-18 10:15:19
  • HTML4标签的默认样式列表

    2007-09-28 22:00:00
  • Vue3中的setup语法糖、computed函数、watch函数详解

    2024-04-29 13:09:33
  • php简单防盗链验证实现方法

    2023-09-12 04:55:15
  • 楼层数横排比竖排好

    2008-04-26 07:28:00
  • asp之家 网络编程 m.aspxhome.com