氣輕PyQt6 11控件互動與槽函數(shù)(有參數(shù))
?
和【氣輕PyQt6 10】的不同是,這里采用有參數(shù)的槽函數(shù)來處理從文本框和標(biāo)簽的互動。
?
from PyQt6 import QtCore, QtGui
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QPalette,QFont, QColor, QIcon, QPixmap
from PyQt6.QtCore import Qt, pyqtSignal
import sys
?
class PyQt611(QWidget):
??? signal = pyqtSignal( str )????????????????????????? #定義一個信號
??? def __init__(self):
??????? super().__init__()
?
??????? self.initUI()
??? def initUI(self):
??????? self.setWindowTitle('氣輕PyQt6')??????????????? # 設(shè)置窗口標(biāo)題
??????? self.resize(430, 60)??????????????????????????? # 設(shè)置窗口大小
?
??????? self.signal.connect(self.updateLabel)?????????? # 連接信號與槽
?
??????? self.edit = QLineEdit(self)???????????????????? # 輸入框
??????? self.edit.setGeometry(10, 0,240, 40)??????????? # 設(shè)置位置和大小
??????? self.edit.setText('槽函數(shù)(有參數(shù))')
??????? self.edit.setStyleSheet('background-color:#FFE4B5;color : #32CD32; \
??????????????????? font: bold italic large /"SimSun/";font-size:25px')
?
??????? img = 'arrowsright.png'???????????????????????? # 按鈕圖標(biāo)
??????? self.btn = QPushButton('', self)
??????? self.btn.setGeometry(250, 0, 40, 40)
??????? self.btn.setIcon(QIcon(QPixmap(img)))
??????? self.btn.setStyleSheet('background-color:#96CDCD;color : #FFE4C4; \
??????????????????? font: bold large /"SimSun/";font-size:16px')
??????? self.btn.clicked.connect(self.buttonClicked)
?
??????? self.tag = QLabel(self)???????????????????????? # 設(shè)置label信息
??????? self.tag.setGeometry(290, 0,120, 40)??????????? # 設(shè)置位置和大小
??????? self.tag.setText('標(biāo)簽')
??????? self.tag.setAlignment(Qt.AlignmentFlag.AlignHCenter)# 居中設(shè)置
??????? self.tag.setToolTip('標(biāo)簽提示')???????????????? # 設(shè)置標(biāo)簽提示
??????????????????????????????????????????????????????? # 背景和前景顏色
??????? self.tag.setStyleSheet('background-color:yellow;color : #6495ED; \
??????????????????? font: bold italic large /"Times New Roman/";font-size:25px')
?
??????? self.show()
?
??? def buttonClicked(self):
??????? edittext = self.edit.text()
??????? self.signal.emit(edittext)??????? ??????????????# 發(fā)射信號
?
??? def updateLabel(self,s):??????????????????????????? # 槽函數(shù)
??????? self.tag.setText(s)
?
if __name__ == '__main__':
??? app = QApplication(sys.argv)
??? window = PyQt611()
??? sys.exit(app.exec())
?
執(zhí)行結(jié)果
