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

2019/7/14 23:01:38

本文主要是介绍pyqt 多窗口之间的相互调用方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

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

# 主窗口
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 多窗口之间的相互调用方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持找一找教程网。



这篇关于pyqt 多窗口之间的相互调用方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程