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使用minimax算法实现五子棋

这是一个命令行环境的五子棋程序。使用了minimax算法。 除了百度各个棋型的打分方式,所有代码皆为本人所撸。本程序结构与之前的井字棋、黑白棋一模一样。 有一点小问题,没时间弄了,就这样...

Python Django框架url反向解析实现动态生成对应的url链接示例

本文实例讲述了Python Django框架url反向解析实现动态生成对应的url链接。分享给大家供大家参考,具体如下: url反向解析:根据url路由规则,动态生成对应的url链...

解决python 读取excel时 日期变成数字并加.0的问题

解决python 读取excel时 日期变成数字并加.0的问题

excel 文件内容如下: 读取excel内容: import xlrd from datetime import datetime from xlrd import xldat...

python内置函数sorted()用法深入分析

本文实例讲述了python内置函数sorted()用法。分享给大家供大家参考,具体如下: 列表对象提供了sort()方法支持原地排序,而内置函数sorted()不支持原地操作只是返回新的...

Python求导数的方法

本文实例讲述了Python求导数的方法。分享给大家供大家参考。具体实现方法如下: def func(coeff): sum='' for key in coeff:...