小编给大家分享一下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的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。