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程序员开发中常犯的10个错误

Python是一门简单易学的编程语言,语法简洁而清晰,并且拥有丰富和强大的类库。与其它大多数程序设计语言使用大括号不一样 ,它使用缩进来定义语句块。   在平时的工作中,Python开发...

分享Pycharm中一些不为人知的技巧

分享Pycharm中一些不为人知的技巧

工欲善其事必先利其器,Pycharm 是最受欢迎的Python开发工具,它提供的功能非常强大,是构建大型项目的理想工具之一,如果能挖掘出里面实用技巧,能带来事半功倍的效果。 以下操作都是...

django 外键model的互相读取方法

先设定一个关系模型如下: from django.db import models class Blog(models.Model): name = models.CharFiel...

Python3写入文件常用方法实例分析

本文实例讲述了Python3写入文件常用方法。分享给大家供大家参考。具体如下: ''''' Created on Dec 18, 2012 写入文件 @author: liur...

Window环境下Scrapy开发环境搭建

Window环境下Scrapy开发环境搭建

快速搭建scrapy开发环境 python pippip 百度网盘 注:不同的电脑上所带有环境不同,安装方式有些许差别 1、成功安装python并添加环境变量 2、安装...