Python 中PyQt5 點(diǎn)擊主窗口彈出另一個窗口的實(shí)現(xiàn)方法
1.先使用Qt designer設(shè)計(jì)兩個窗口,一個是主窗口,一個是子窗口
![]() |
其中主窗口是新建-Main Window,子窗口是Dialog窗體。
兩個窗口不能是同一類型,否則會崩潰。
并保存為EyeTracking_main.ui和EyeTracking_process.ui(因?yàn)槲以谧鲅蹌幼粉?,因此窗體命名與此相關(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_())
因?yàn)楹罄m(xù)還要在各個窗體操作,因此將主窗口與子窗口各自實(shí)例化在parentWindow和childWindow兩個類中,這兩個類各自繼承了QMainWindow和QDialog的父類:
class parentWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)#QMainWindow的初始化
self.main_ui = Ui_MainWindow()#主窗口的實(shí)例化
self.main_ui.setupUi(self)
class childWindow(QDialog):
def __init__(self):
QDialog.__init__(self)
self.child=Ui_Dialog()#子窗口的實(shí)例化
self.child.setupUi(self)
后面分別再把兩個窗口實(shí)例化給window和child:
window=parentWindow() child=childWindow()
通過定義按鈕意義將兩個窗體關(guān)聯(lián)起來:
btn=window.main_ui.toolButton btn.clicked.connect(child.show)
表示當(dāng)按鈕按下時,子窗口顯示。
如下圖,當(dāng)點(diǎn)擊“處理眼動數(shù)據(jù)”,彈出處理處理窗體:

總結(jié)
以上所述是小編給大家介紹的Python 中PyQt5 點(diǎn)擊主窗口彈出另一個窗口的實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
python?lazypredict構(gòu)建大量基本模型簡化機(jī)器學(xué)習(xí)
這篇文章主要介紹了python?lazypredict構(gòu)建大量基本模型簡化機(jī)器學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
ubuntu16.04升級Python3.5到Python3.7的方法步驟
這篇文章主要介紹了ubuntu16.04升級Python3.5到Python3.7的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
python實(shí)現(xiàn)給數(shù)組按片賦值的方法
這篇文章主要介紹了python實(shí)現(xiàn)給數(shù)組按片賦值的方法,實(shí)例分析了Python在指定位置進(jìn)行賦值的相關(guān)技巧,需要的朋友可以參考下2015-07-07
Swin?Transformer圖像處理深度學(xué)習(xí)模型
這篇文章主要為大家介紹了Swin?Transformer圖像處理深度學(xué)習(xí)模型詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Django數(shù)據(jù)庫操作的實(shí)例(增刪改查)
下面小編就為大家?guī)硪黄狣jango數(shù)據(jù)庫操作的實(shí)例(增刪改查)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
Python 開發(fā)工具PyCharm安裝教程圖文詳解(新手必看)
PyCharm是一種Python IDE,帶有一整套可以幫助用戶在使用Python語言開發(fā)時提高其效率的工具,比如調(diào)試、語法高亮、Project管理、代碼跳轉(zhuǎn)、智能提示、自動完成、單元測試、版本控制。今天通過本文給大家分享PyCharm安裝教程,一起看看吧2020-02-02
Python Tricks 使用 pywinrm 遠(yuǎn)程控制 Windows 主機(jī)的方法
這篇文章主要介紹了Python Tricks 使用 pywinrm 遠(yuǎn)程控制 Windows 主機(jī)的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07


