Python 中PyQt5 点击主窗口弹出另一个窗口的实现方法

yipeiwu_com6年前Python基础

1.先使用Qt designer设计两个窗口,一个是主窗口,一个是子窗口

 

 

其中主窗口是新建-Main Window,子窗口是Dialog窗体。

  两个窗口不能是同一类型,否则会崩溃。

并保存为EyeTracking_main.uiEyeTracking_process.ui(因为我在做眼动追踪,因此窗体命名与此相关,后同),使用UIC工具转成.py文件。

2.写一个驱动函数调用两个窗体

主窗体Eyetracking_main.py

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(954, 530)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.toolButton = QtWidgets.QToolButton(self.centralwidget)
    self.toolButton.setGeometry(QtCore.QRect(10, 40, 101, 25))   self.toolButton.setObjectName("toolButton")
    ...1234567891011

子窗体Eyetracking_process.py

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
  def setupUi(self, Dialog):
    Dialog.setObjectName("Dialog")
    Dialog.resize(810, 474)
    self.label_5 = QtWidgets.QLabel(Dialog)
    self.label_5.setGeometry(QtCore.QRect(630, 90, 151, 151))
    self.label_5.setObjectName("label_5")
    self.label_2 = QtWidgets.QLabel(Dialog)
    self.label_2.setGeometry(QtCore.QRect(250, 90, 171, 161))
    self.label_2.setObjectName
("label_2")
        ...12345678910111213

将驱动函数命名为EyeTracking_ui.py

from Eyetracking_main import *
from Eyetracking_process import *
from PyQt5.QtWidgets import QApplication,QMainWindow,QDialog
import sys
class parentWindow(QMainWindow):
  def __init__(self):
    QMainWindow.__init__(self)
    self.main_ui = Ui_MainWindow()
    self.main_ui.setupUi(self)
class childWindow(QDialog):
  def __init__(self):
    QDialog.__init__(self)
    self.child=Ui_Dialog()
    self.child.setupUi(self)

if __name__=='__main__':
  app=QApplication(sys.argv)
  window=parentWindow()
  child=childWindow()
  #通过toolButton将两个窗体关联
  btn=window.main_ui.toolButton
  btn.clicked.connect(child.show)
  # 显示
  window.show()
  sys.exit(app.exec_())

因为后续还要在各个窗体操作,因此将主窗口与子窗口各自实例化在parentWindowchildWindow两个类中,这两个类各自继承了QMainWindow和QDialog的父类:

class parentWindow(QMainWindow):
  def __init__(self):
    QMainWindow.__init__(self)#QMainWindow的初始化
    self.main_ui = Ui_MainWindow()#主窗口的实例化
    self.main_ui.setupUi(self)
class childWindow(QDialog):
  def __init__(self):
    QDialog.__init__(self)
    self.child=Ui_Dialog()#子窗口的实例化
    self.child.setupUi(self)

后面分别再把两个窗口实例化给window和child:

window=parentWindow()
child=childWindow()

通过定义按钮意义将两个窗体关联起来:

btn=window.main_ui.toolButton
btn.clicked.connect(child.show)

表示当按钮按下时,子窗口显示。

如下图,当点击“处理眼动数据”,弹出处理处理窗体:

总结

以上所述是小编给大家介绍的Python 中PyQt5 点击主窗口弹出另一个窗口的实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

Python OpenCV利用笔记本摄像头实现人脸检测

Python OpenCV利用笔记本摄像头实现人脸检测

本文实例为大家分享了Python OpenCV利用笔记本摄像头实现人脸检测的具体代码,供大家参考,具体内容如下 1.安装opencv 首先参考其他文章安装pip。 之后以管理员身份运行命...

使用python 写一个静态服务(实战)

使用python 写一个静态服务(实战)

师父布置的任务,让我写一个服务练练手,搞清楚socket的原理和过程后跑了一个小demo,很有成就感,代码内容也比较清晰易懂,很有教育启发意义。 代码 # coding:utf-8...

python3 遍历删除特定后缀名文件的方法

python3 遍历删除特定后缀名文件的方法

U盘中毒了,U盘内的每个文件夹内都多了一个.lnk文件,处女座又犯了,实在不能忍,就写了个脚本把所有的.lnk文件删除了。 多级目录递归删除 import os n = 0 for...

python冒泡排序算法的实现代码

1.算法描述:(1)共循环 n-1 次(2)每次循环中,如果 前面的数大于后面的数,就交换(3)设置一个标签,如果上次没有交换,就说明这个是已经好了的。 2.python冒泡排序代码 复...

使用pip发布Python程序的方法步骤

使用pip发布Python程序的方法步骤

写过 Python 程序的小伙伴们都知道,需要 import 个非 Python 自带的软件包时,都要用到 pip 这个程序。平时我们都是用 pip,如果我们写好了一个程序,想让大家都能...