Python 中PyQt5 點擊主窗口彈出另一個窗口的實現(xiàn)方法
1.先使用Qt designer設(shè)計兩個窗口,一個是主窗口,一個是子窗口
![]() |
其中主窗口是新建-Main Window,子窗口是Dialog窗體。
兩個窗口不能是同一類型,否則會崩潰。
并保存為EyeTracking_main.ui和EyeTracking_process.ui(因為我在做眼動追蹤,因此窗體命名與此相關(guān),后同),使用UIC工具轉(zhuǎn)成.py文件。
2.寫一個驅(qū)動函數(shù)調(diào)用兩個窗體
主窗體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
將驅(qū)動函數(shù)命名為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將兩個窗體關(guān)聯(lián)
btn=window.main_ui.toolButton
btn.clicked.connect(child.show)
# 顯示
window.show()
sys.exit(app.exec_())
因為后續(xù)還要在各個窗體操作,因此將主窗口與子窗口各自實例化在parentWindow和childWindow兩個類中,這兩個類各自繼承了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()
通過定義按鈕意義將兩個窗體關(guān)聯(lián)起來:
btn=window.main_ui.toolButton btn.clicked.connect(child.show)
表示當(dāng)按鈕按下時,子窗口顯示。
如下圖,當(dāng)點擊“處理眼動數(shù)據(jù)”,彈出處理處理窗體:

總結(jié)
以上所述是小編給大家介紹的Python 中PyQt5 點擊主窗口彈出另一個窗口的實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Python中高效抓取數(shù)據(jù)的實戰(zhàn)指南
在數(shù)據(jù)驅(qū)動的時代,網(wǎng)絡(luò)爬蟲已成為獲取信息的核心工具,本文將用通俗的語言,帶您掌握Python爬蟲結(jié)合代理IP抓取數(shù)據(jù)的全流程,希望對大家有一定的幫助2025-04-04
Python3自動簽到 定時任務(wù) 判斷節(jié)假日的實例
今天小編就為大家分享一篇Python3自動簽到 定時任務(wù) 判斷節(jié)假日的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Python結(jié)合MySQL數(shù)據(jù)庫編寫簡單信息管理系統(tǒng)完整實例
最近Python課堂上布置了綜合實訓(xùn),實驗?zāi)繕?biāo)是設(shè)計一個信息管理系統(tǒng),下面這篇文章主要給大家介紹了關(guān)于Python結(jié)合MySQL數(shù)據(jù)庫編寫簡單信息管理系統(tǒng)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
python網(wǎng)絡(luò)爬蟲之模擬登錄 自動獲取cookie值 驗證碼識別的具體實現(xiàn)
有時,我們需要爬取一些基于個人用戶的用戶信息(需要登陸后才可以查看)就要進(jìn)行模擬登陸,因為驗證碼往往是作為登陸請求中的請求參數(shù)被使用,就需要識別驗證碼2021-09-09
python神經(jīng)網(wǎng)絡(luò)InceptionV3模型復(fù)現(xiàn)詳解
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)InceptionV3模型復(fù)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2022-05-05


