pyqt 多窗口之間的相互調(diào)用方法
* 在編程開(kāi)發(fā)中,一個(gè)程序不可避免的需要多窗口操作來(lái)實(shí)現(xiàn)具體的功能。
實(shí)現(xiàn)此功能的基本步驟(以三個(gè)窗口為例,使用主窗口調(diào)用其它兩個(gè)窗口)
# 主窗口
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", "打開(kāi)窗口1"))
self.pushButton_2.setText(_translate("MainWindow", "打開(kāi)窗口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_())
以上實(shí)現(xiàn)主窗口通過(guò)按鈕彈出窗口1和窗口2
下面實(shí)現(xiàn)通過(guò)窗口按鈕打開(kāi)文件資源管理器,實(shí)現(xiàn)獲取文件相關(guān)信息的功能:
1. 在主窗口中添加一個(gè)按鈕:
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", "打開(kāi)目錄"))
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 多窗口之間的相互調(diào)用方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python+SimpleRNN實(shí)現(xiàn)股票預(yù)測(cè)詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Python和SimpleRNN實(shí)現(xiàn)股票預(yù)測(cè)效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定幫助,需要的可以參考一下2022-05-05
Python設(shè)計(jì)密碼強(qiáng)度校驗(yàn)程序
這篇文章主要介紹了Python如何設(shè)計(jì)密碼強(qiáng)度校驗(yàn)程序,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
基python實(shí)現(xiàn)多線(xiàn)程網(wǎng)頁(yè)爬蟲(chóng)
python是支持多線(xiàn)程的, 主要是通過(guò)thread和threading這兩個(gè)模塊來(lái)實(shí)現(xiàn)的,本文主要給大家分享python實(shí)現(xiàn)多線(xiàn)程網(wǎng)頁(yè)爬蟲(chóng),需要的朋友可以參考下2015-09-09
Python動(dòng)態(tài)創(chuàng)建類(lèi)實(shí)例詳解
這篇文章主要為大家介紹了Python動(dòng)態(tài)創(chuàng)建類(lèi)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
使用?Flask、Celery?和?Python?實(shí)現(xiàn)每月定時(shí)任務(wù)的步驟
下面給大家分享使用?Flask、Celery?和?Python?實(shí)現(xiàn)每月定時(shí)任務(wù)的步驟,本文分步驟結(jié)合腳本給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08
Python使用ffmpy將amr格式的音頻轉(zhuǎn)化為mp3格式的例子
今天小編就為大家分享一篇Python使用ffmpy將amr格式的音頻轉(zhuǎn)化為mp3格式的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
python 實(shí)現(xiàn)文件的遞歸拷貝實(shí)現(xiàn)代碼
今天翻電腦時(shí)突然發(fā)現(xiàn)有個(gè)存了很多照片和視頻的文件夾,想起來(lái)是去年換手機(jī)(流行的小5)時(shí)拷出來(lái)的??戳藥讖堈掌掠忠荒荒坏母‖F(xiàn)在腦海,好吧,我是個(gè)感性的人2012-08-08
通過(guò)實(shí)例解析python and和or使用方法
這篇文章主要介紹了通過(guò)實(shí)例解析python and和or使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
python 淺談serial與stm32通信的編碼問(wèn)題
今天小編就為大家分享一篇python 淺談serial與stm32通信的編碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12

