氣輕PyQt6 07 標(biāo)簽(QLabel) 與文本框互動
?
在文本框中輸入回車時,可以將文本框中的文字設(shè)置到標(biāo)簽中去。
?
from PyQt6 import QtCore, QtGui
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QPalette,QFont, QColor
from PyQt6.QtCore import Qt
import sys
?
class PyQt607(QWidget):
??? def __init__(self):
??????? super().__init__()
??????? self.initUI()
??? def initUI(self):
??????? self.setWindowTitle('氣輕PyQt6')??????????????? # 設(shè)置窗口標(biāo)題
??????? self.resize(700, 300)???????? ??????????????????# 設(shè)置窗口大小
?
??????? self.edit = QLineEdit(self)???????????????????? # 輸入框
??????? self.edit.setGeometry(10, 0,240, 40)??????????? # 設(shè)置位置和大小
??????? self.edit.setText('edit')
??????? self.edit.setFont(QFont('Arial',20))
??????? self.edit.returnPressed.connect(self.updateLabel)
?
??????? self.tag = QLabel(self)???????????????????????? # 設(shè)置label信息
??????? self.tag.setGeometry(290, 0,120, 40)??????????? # 設(shè)置位置和大小
??????? self.tag.setText('標(biāo)簽')
??????? self.tag.setToolTip('標(biāo)簽提示')???? ????????????# 設(shè)置標(biāo)簽提示
??????? self.tag.setAlignment(Qt.AlignmentFlag.AlignHCenter)# 居中設(shè)置
??????? self.tag.setStyleSheet('background-color:yellow;color : #6495ED; \
??????????? font: bold italic large /"Times New Roman/"; font-size:25px')
?
??????? self.show()
?
??? def updateLabel(self):
??????? edittext = self.edit.text()
??????? self.tag.setText(edittext)
?
if __name__ == '__main__':
??? app = QApplication(sys.argv)
??? window = PyQt607()
??? sys.exit(app.exec())
?
