鄭州做網(wǎng)站公司有多少泰州網(wǎng)站建設(shè)優(yōu)化
? ? ? ? 在上一篇文章中python之pyqt專欄6-信號(hào)與槽2-CSDN博客中,我們可以了解到對(duì)象可以使用內(nèi)置信號(hào),這些信號(hào)來自于類定義或者繼承過來的。我們可以對(duì)這些信號(hào)可以通過connect連接槽函數(shù)。
需求
? ? ? ? 現(xiàn)在有一個(gè)需求,有兩個(gè)UI界面“untitled.ui”和“untitled1.ui”,untitled.ui有一個(gè)lineEdit(行編輯)和一個(gè)button(按鈕),untitled1.ui有一個(gè)Label。點(diǎn)擊untitled.ui的button時(shí),將行編輯的文本內(nèi)容,設(shè)置為untitled1.ui的Label文本內(nèi)容。
untitled.ui的對(duì)象列表
對(duì)象名 | 類型 |
lineEdit | LlineEdit |
pushButton | QPushButto |
untitled1.ui的對(duì)象列表
對(duì)象名 | 類型 |
label | QLabel |
UI界面設(shè)置
untitled.ui? UI界面
? ? ? ?保存文件為untitled.ui
untitled1.ui? UI界面
? ? ? ? 點(diǎn)擊左上角“文件”->“新建”
????????保存文件為untitled.ui?
? ? ? ? ?注:Qt Designer中,當(dāng)有兩個(gè)以上的UI編輯界面時(shí),需要先選中的UI界面,再保存
?項(xiàng)目目錄下“untitled.ui”和“untitled1.ui”轉(zhuǎn)換為“untitled.py”和“untitled1.py”
main.py
# 導(dǎo)入sys模塊
import sys
# PyQt6.QtWidgets模塊中導(dǎo)入QApplication, QWidget
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtCore import QObjectimport untitled
import untitled1class MyMainForm(QWidget, untitled.Ui_Form):sendText = pyqtSignal(str)def __init__(self, parent=None):# 調(diào)用父類的構(gòu)造函數(shù)super(MyMainForm, self).__init__(parent)# 調(diào)用繼承Ui_Form過來的setupUi函數(shù)self.setupUi(self)self.pushButton.clicked.connect(self.btn_clicked)class MyMainForm1(QWidget, untitled1.Ui_Form):def __init__(self, parent=None):# 調(diào)用父類的構(gòu)造函數(shù)super(MyMainForm1, self).__init__(parent)# 調(diào)用繼承Ui_Form過來的setupUi函數(shù)self.setupUi(self)self.move(1200,320)# Press the green button in the gutter to run the script.
if __name__ == '__main__':# 實(shí)例化應(yīng)用app = QApplication(sys.argv)# 實(shí)例化MyMainFormmyw = MyMainForm()myw.show()myw1 = MyMainForm1()myw1.show()myw.sendText.connect(myw1.deal_signal)# 啟動(dòng)應(yīng)用程序的事件循環(huán)并等待用戶交互,直到應(yīng)用程序關(guān)閉。sys.exit(app.exec())
? ? ? ? 防止兩個(gè)窗口重疊,在MyMainForm1移動(dòng)一下位置
self.move(1200,320)
??
????????有兩個(gè)窗口,建立了兩個(gè)類MyMainForm與MyMainForm1,它們分別繼承于untitled.Ui_Form與untitled1.Ui_Form
? ? ? ? 需要注意的是untitled.py與untitled1.py都有Ui_Form,為了區(qū)分Ui_Form來源,不能用如下代碼,否者會(huì)被Ui_Form會(huì)被后面的取代
from untitled import Ui_Form
from untitled1 import Ui_Form
?????????正確書寫應(yīng)該是這樣
import untitled
import untitled1
class MyMainForm(QWidget, untitled.Ui_Form):
class MyMainForm1(QWidget, untitled1.Ui_Form):
? 問題
? ? ? ? 在MyMainForm,button被點(diǎn)擊時(shí)會(huì)發(fā)出clicked信號(hào),如果用將button的clicked信號(hào),綁定槽函數(shù),在這個(gè)槽函數(shù)里面可以實(shí)現(xiàn)獲取lineEdit的文本內(nèi)容,代碼如下
self.pushButton.clicked.connect(self.btn_clicked)
def btn_clicked(self):# 獲取行編輯文本str = self.lineEdit.text()
????????MyMainForm與MyMainForm1,它們是兩個(gè)類,沒有直接關(guān)系,這個(gè)槽函數(shù)在MyMainForm中,不能修改MyMainForm1的label,也就是不能通過如下代碼
def btn_clicked(self):# 獲取行編輯文本str = self.lineEdit.text()self.label.setText(str)
?自定義信號(hào)
? ????????如果我們可以在untitled.py的Ui_Form自定義一個(gè)信號(hào)(sendText),這個(gè)信號(hào)通過connect綁定untitled1.py的Ui_Form類函數(shù)(deal_signal),那么它們就會(huì)建立關(guān)系。
myw.sendText.connect(myw1.deal_signal)
修改代碼如下
# 導(dǎo)入sys模塊
import sys
# PyQt6.QtWidgets模塊中導(dǎo)入QApplication, QWidget
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtCore import QObject, pyqtSignalimport untitled
import untitled1class MyMainForm(QWidget, untitled.Ui_Form):sendText = pyqtSignal(str)def __init__(self, parent=None):# 調(diào)用父類的構(gòu)造函數(shù)super(MyMainForm, self).__init__(parent)# 調(diào)用繼承Ui_Form過來的setupUi函數(shù)self.setupUi(self)self.pushButton.clicked.connect(self.btn_clicked)def btn_clicked(self):# 獲取行編輯文本str = self.lineEdit.text()self.sendText.emit(str)class MyMainForm1(QWidget, untitled1.Ui_Form):def __init__(self, parent=None):# 調(diào)用父類的構(gòu)造函數(shù)super(MyMainForm1, self).__init__(parent)# 調(diào)用繼承Ui_Form過來的setupUi函數(shù)self.setupUi(self)self.move(1200,320)def deal_signal(self,str):self.label.setText(str)# Press the green button in the gutter to run the script.
if __name__ == '__main__':# 實(shí)例化應(yīng)用app = QApplication(sys.argv)# 實(shí)例化MyMainFormmyw = MyMainForm()myw.show()myw1 = MyMainForm1()myw1.show()myw.sendText.connect(myw1.deal_signal)# 啟動(dòng)應(yīng)用程序的事件循環(huán)并等待用戶交互,直到應(yīng)用程序關(guān)閉。sys.exit(app.exec())
??????自定義信號(hào)過程
? ? ? 1)導(dǎo)入?pyqtSignal類?
from PyQt6.QtCore import pyqtSignal
? ? ? ? 2)定義類中信號(hào)屬性,“str”是參數(shù)
sendText = pyqtSignal(str)
? ? ? 3)信號(hào)與槽綁定
myw.sendText.connect(myw1.deal_signal)
? ? ? ?4)發(fā)送信號(hào)
self.sendText.emit(str)
? ? ? ? ? 在該項(xiàng)目功能需求中,需要獲取MyMainForm的lineEdit的內(nèi)容,將其內(nèi)容傳遞傳遞給MyMainForm1的deal_signal,并在deal_signal對(duì)MyMainForm1的文本設(shè)置,因此需要形參“str”,如果自定義信號(hào)不需要傳遞內(nèi)容,則不需要添形參,如下代碼即可
sendText = pyqtSignal()
最終實(shí)現(xiàn)
?????????