PyQt5学习笔记-从主窗体打开一个子窗体

PyQt5学习笔记-从主窗体打开一个子窗体

软件环境:

Eric6+Python3.5+PyQt5

试验目标:

1.点击菜单项Open,打开一个子窗体

2.点击按钮Open,打开一个子窗体

主窗体设计:

1、增加一个菜单项Open

2、增加一个按钮Open

子窗体设计:

1、只放一个按钮Exit

设计步骤

1、创建主窗体

启动Eric6,点击顶部菜单Project,点击New,给出Project Name,同时设置项目路径,然后点下面的OK按钮

PyQt5学习笔记-从主窗体打开一个子窗体

2、点击如下图的1图标,然后点New form

PyQt5学习笔记-从主窗体打开一个子窗体

3、Select a form type,选择Main Window

PyQt5学习笔记-从主窗体打开一个子窗体

4.文件名,设置为mainWindow,保存后,则是MainWindow.ui

5、增加菜单,在窗体区域,点鼠标右键,点“创建菜单栏”,这里只增加一个主菜单项File,两个子菜单项Open和Exit

PyQt5学习笔记-从主窗体打开一个子窗体

6、在窗体上放置一个按钮Open,从左侧栏找到push button,拖拽到窗体上,修改按钮属性,objectName:btn_Open,Text:Open

PyQt5学习笔记-从主窗体打开一个子窗体

7、然后点左上角的保存按钮,关闭Qt Designer

PyQt5学习笔记-从主窗体打开一个子窗体

8、增加第子窗体,参照前面第2步,点New form,然后Select a form type,选择Widget,文件名:ChildForm.ui

PyQt5学习笔记-从主窗体打开一个子窗体

9、在这个子窗体上增加一个按钮Exit ,从左侧栏拖一个push button,调整按钮大小,修改属性,objectName:btn_Exit,Text:Exit

点左上角的保存按钮,保存这个窗体文件,然后关闭Qt Designer

PyQt5学习笔记-从主窗体打开一个子窗体

10、编译窗体,从XXX.ui文件生成ui_XXX.py文件

PyQt5学习笔记-从主窗体打开一个子窗体

依次选中这两个文件,点鼠标右键,点Compile form

点窗体左侧的页签图标,可以看到增加了两个文件:Ui_MainWindow.py、Ui_ChildForm.py

PyQt5学习笔记-从主窗体打开一个子窗体

11、Ui_MainWindow.py和Ui_ChildForm.py是界面文件,下面增加业务逻辑文件

PyQt5学习笔记-从主窗体打开一个子窗体页签图标,选中MainWindow.ui文件,点鼠标右键,点选Generate Dialog Code菜单项

PyQt5学习笔记-从主窗体打开一个子窗体

12、点New 按钮,className 为MainWindow,然后点OK按钮

PyQt5学习笔记-从主窗体打开一个子窗体

13、绑定槽函数,

1)、绑定菜单项槽函数,选择on_ActionXXX_triggered()

依次点开actionExit(QAction)、actionOpen(QAction),勾选onAction_Exit_triggered()、onAction_Open_triggered()

PyQt5学习笔记-从主窗体打开一个子窗体

2)、绑定按钮槽函数,点开btn_Open(QPushButton),勾选on_btn_Open_clicked()

PyQt5学习笔记-从主窗体打开一个子窗体

3)、给子窗体绑定槽函数,选中Ui_ChildForm.ui,点鼠标右键,选中Generate Dialog Code菜单项,className命名为ChildForm

勾选on_btn_Exit_clicked()

PyQt5学习笔记-从主窗体打开一个子窗体

14、重新编译两个窗体文件

选中一个ui文件,然后点鼠标右键,点选Compile all forms,生成代码文件

PyQt5学习笔记-从主窗体打开一个子窗体

PyQt5学习笔记-从主窗体打开一个子窗体页签,则可以看到,系统新增了两个文件MainWindow.py和ChildForm.py

PyQt5学习笔记-从主窗体打开一个子窗体

15、开始完善代码

双击MainWindow.py,打开这个文件,编辑代码,进行相应完善,项目启动,从MainWindow.py进行启动,需要实例化窗体界面类,包括主窗体和子窗体

修改内容:

1)、把.Ui_MainWindow前面的点号去掉,不然运行会报错

PyQt5学习笔记-从主窗体打开一个子窗体

2)、导入子窗体

增加语句:from Ui_ChildForm import Ui_ChildForm

16、附相关文件的代码:

1)、MainWindow.py

# -*- coding: utf-8 -*-

"""
Module implementing MainWindow.
""" from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QWidget
from PyQt5 import QtWidgets from Ui_MainWindow import Ui_MainWindow
from Ui_ChildForm import Ui_ChildForm class MainWindow(QMainWindow, Ui_MainWindow):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
"""
Constructor @param parent reference to the parent widget
@type QWidget
"""
super(MainWindow, self).__init__(parent)
self.setupUi(self) @pyqtSlot()
def on_btn_Open_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
#raise NotImplementedError
ch.show() @pyqtSlot()
def on_actionOpen_triggered(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
#raise NotImplementedError
ch.show() @pyqtSlot()
def on_actionExit_triggered(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
#raise NotImplementedError
sys.exit(0) class ChildForm(QWidget, Ui_ChildForm):
def __init__(self):
#super(QWidget, self).__init__(parent)
super(QWidget, self).__init__()
self.setupUi(self)
self.btn_Exit.clicked.connect(self.close) if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv) #实例化主窗体
mainForm = MainWindow() #显示主窗体
mainForm.show() #实例化子窗体
ch=ChildForm() sys.exit(app.exec_())

2)、Ui_MainWindow.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'C:\Work\Python\PyQt5\Test\MainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.btn_Open = QtWidgets.QPushButton(self.centralWidget)
self.btn_Open.setGeometry(QtCore.QRect(210, 120, 81, 31))
self.btn_Open.setObjectName("btn_Open")
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menuBar.setObjectName("menuBar")
self.menuFile = QtWidgets.QMenu(self.menuBar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menuBar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionExit)
self.menuBar.addAction(self.menuFile.menuAction()) self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.btn_Open.setText(_translate("MainWindow", "Open"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionExit.setText(_translate("MainWindow", "Exit")) if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

3)、ChildForm.py

# -*- coding: utf-8 -*-

"""
Module implementing ChildForm.
""" from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QWidget from Ui_ChildForm import Ui_Form class ChildForm(QWidget, Ui_Form):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
"""
Constructor @param parent reference to the parent widget
@type QWidget
"""
super(ChildForm, self).__init__(parent)
self.setupUi(self) @pyqtSlot()
def on_btn_Exit_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
raise NotImplementedError

4)、Ui_ChildForm.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'C:\Work\Python\PyQt5\Test\ChildForm.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_ChildForm(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.btn_Exit = QtWidgets.QPushButton(Form)
self.btn_Exit.setGeometry(QtCore.QRect(140, 110, 91, 41))
self.btn_Exit.setObjectName("btn_Exit") self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.btn_Exit.setText(_translate("Form", "Exit")) if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_ChildForm()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())

本示例测试可以运行:

运行,点击顶部的运行图标

PyQt5学习笔记-从主窗体打开一个子窗体

设置运行使用的Python解释器及命令行,Python 本项目MainWindow.py的路径,点OK按钮

PyQt5学习笔记-从主窗体打开一个子窗体

运行结果:

PyQt5学习笔记-从主窗体打开一个子窗体

点菜单项里的Open或点Open按钮,则弹出子窗体

PyQt5学习笔记-从主窗体打开一个子窗体

点子窗体里的Exit按钮,则关闭子窗体

代码下载,百度网盘分享:
https://pan.baidu.com/s/1LzUVY2uPggEktCtACwC-JQ

提取码:i0qq

上一篇:WPF一步步开发XMPP IM客户端2:主窗体设计


下一篇:Android 后台线程,timertask实现定期更新时间