pyqt 多窗口之间的相互调用方法

yipeiwu_com6年前Python基础

* 在编程开发中,一个程序不可避免的需要多窗口操作来实现具体的功能。

实现此功能的基本步骤(以三个窗口为例,使用主窗口调用其它两个窗口)

# 主窗口
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.pushButton = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(70, 180, 75, 23))
    self.pushButton.setObjectName("pushButton")
 
    self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_2.setGeometry(QtCore.QRect(250, 180, 75, 23))
    self.pushButton_2.setObjectName("pushButton_2")
 
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
    self.menubar.setObjectName("menubar")
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)
 
    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)
 
  def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.pushButton.setText(_translate("MainWindow", "打开窗口1"))
    self.pushButton_2.setText(_translate("MainWindow", "打开窗口2 "))
 
# 窗口1
class Ui_Dialog(object):
  def setupUi(self, Dialog):
    Dialog.setObjectName("Dialog")
    Dialog.resize(400, 300)
    self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
    self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
    self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
    self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
    self.buttonBox.setObjectName("buttonBox")
    self.label = QtWidgets.QLabel(Dialog)
    self.label.setGeometry(QtCore.QRect(140, 100, 54, 12))
    self.label.setObjectName("label")
 
    self.retranslateUi(Dialog)
    self.buttonBox.accepted.connect(Dialog.accept)
    self.buttonBox.rejected.connect(Dialog.reject)
    QtCore.QMetaObject.connectSlotsByName(Dialog)
 
  def retranslateUi(self, Dialog):
    _translate = QtCore.QCoreApplication.translate
    Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
    self.label.setText(_translate("Dialog", "这是窗口1"))
# 窗口2
 
class Ui_Form(object):
  def setupUi(self, Form):
    Form.setObjectName("Form")
    Form.resize(400, 300)
    self.label = QtWidgets.QLabel(Form)
    self.label.setGeometry(QtCore.QRect(140, 140, 54, 12))
    self.label.setObjectName("label")
 
    self.retranslateUi(Form)
    QtCore.QMetaObject.connectSlotsByName(Form)
 
  def retranslateUi(self, Form):
    _translate = QtCore.QCoreApplication.translate
    Form.setWindowTitle(_translate("Form", "Form"))
    self.label.setText(_translate("Form", "这是窗口2"))
 

主程序入口:

# 主程序
class MainWindow(QMainWindow, untitled.Ui_MainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()
    self.setupUi(self)
    self.window2 = Ui_Dialog()
    self.window2.setupUi()
    self.window3 = Ui_Form()
    self.window3.setupUi()
    self.pushButton.clicked.connect(self.window2.show)# 绑定窗口2 
    self.pushButton_2.clicked.connect(self.window3.show) # 绑定窗口3
 
 
if __name__ == '__main__':
  app = QApplication(sys.argv)
  MainWindow = MainWindow()
  MainWindow.show()
  sys.exit(app.exec_())

以上实现主窗口通过按钮弹出窗口1和窗口2

下面实现通过窗口按钮打开文件资源管理器,实现获取文件相关信息的功能:

1. 在主窗口中添加一个按钮:

class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    ......
    self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_3.setGeometry(QtCore.QRect(420, 180, 75, 23))
    self.pushButton_3.setObjectName("pushButton_3")
  def retranslateUi(self, MainWindow):
    ......
    self.pushButton_3.setText(_translate("MainWindow", "打开目录"))

2.主程序中添加:

# 主程序
class MainWindow(QMainWindow, untitled.Ui_MainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()
    self.setupUi(self)
    self.window2 = Ui_Dialog()
    self.Window2.setupUi()
    self.window3 = Ui_Form()
    self.Window3.setupUi()
    self.pushButton.clicked.connect(self.window2.show)# 绑定窗口2 
    self.pushButton_2.clicked.connect(self.window3.show) # 绑定窗口3
 
    self.pushButton_3.clicked.connect(self.gefilename) # 新增加的
  # 新增加的
  def gefilename(self):
    filename = QFileDialog.getOpenFileName()
    return filename
 
 
if __name__ == '__main__':
  app = QApplication(sys.argv)
  MainWindow = MainWindow()
  MainWindow.show()
  sys.exit(app.exec_())

即可完成上述功能。

以上这篇pyqt 多窗口之间的相互调用方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python 自动安装 Rising 杀毒软件

不能使用时,或重新安装系统时,方便我们重新安装最新的版本. 但是每次安装都要点击好几次 Next 按钮,同时还要提供序列号,ID 等信息,我很讨厌这种重复工作,索性写一个小的脚本,让他自...

Python容器使用的5个技巧和2个误区总结

Python容器使用的5个技巧和2个误区 “容器”这两个字很少被 Python 技术文章提起。一看到“容器”,大家想到的多是那头蓝色小鲸鱼:Docker,但这篇文章和它没有任何关系。本文...

python实现支付宝当面付(扫码支付)功能

本文实例为大家分享了python实现支付宝当面付示的具体代码,供大家参考,具体内容如下 一、配置信息准备 登录蚂蚁金服开放平台:https://open.alipay.com/platf...

使用IronPython把Python脚本集成到.NET程序中的教程

从两个优秀的世界各取所需,更高效的复用代码。想想就醉了,.NET和python融合了。“懒惰”的程序员们,还等什么? Jesse Smith为您展示如何两个语言来服务同一个.NET程序。...

python kafka 多线程消费者&手动提交实例

官方文档:https://kafka-python.readthedocs.io/en/master/apidoc/KafkaConsumer.html import threadi...