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

2019-08-0709:37:54后端程序开发Comments3,896 views字数 1368阅读模式

PyQt是一个创建GUI应用程序的工具包。它是Python编程语言和Qt库的成功融合。Qt库是目前最强大的库之一。PyQt是由Phil Thompson 开发。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15096.html

我们通过Qt Designer设计两个窗口,命名为主窗口(MainForm)和子窗口(ChildrenForm)。我们在主窗口的空白中央添加一个栅格布局并命名为MaingridLayout,等会需要将ChildrenForm放进去。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15096.html

编写代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15096.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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_())
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15096.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/15096.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定