氣輕PyQt5 08 按鈕(QPushButton)與文本框,標簽互動
?
點擊按鈕后,可以將文本框中的文字設(shè)置到標簽中去。另外,在文本框中輸入回車時也能夠進行同樣操作。
?
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPalette,QFont
from PyQt5.QtCore import Qt
import sys
?
class PyQt508(QWidget):
??? def __init__(self):
??????? super().__init__()
??????? self.initUI()
??? def initUI(self):
??????? self.setWindowTitle('氣輕PyQt5')??????????????? # 設(shè)置窗口標題
??????? self.resize(420, 50)??????????????????????????? # 設(shè)置窗口大小
?
??????? self.edit = QLineEdit(self)???????????????????? # 輸入框
??????? self.edit.setGeometry(10, 0,240, 40)??????????? # 設(shè)置位置和大小
??????? self.edit.setText('edit')
??????? self.edit.setStyleSheet('background-color:#FFE4B5;color : #32CD32; \
??????????????????? font: bold italic large /"Times New Roman/";font-size:25px')
??????? self.edit.returnPressed.connect(self.updateLabel)
?
??????? self.btn = QPushButton('按鈕', self)
??????? self.btn.setGeometry(250, 0, 40, 40)
??????? self.btn.setStyleSheet('background-color:#96CDCD;color : #FFE4C4; \
??????????????????? font: bold large /"SimSun/";font-size:16px')
??????? self.btn.clicked.connect(self.updateLabel)
?
??????? self.tag = QLabel(self)???????????????????????? # 設(shè)置label信息
??????? self.tag.setGeometry(290, 0,120, 40)??????????? # 設(shè)置位置和大小
??????? self.tag.setText('標簽')
??????? self.tag.setToolTip('標簽提示')???????????????? # 設(shè)置標簽提示
??????? self.tag.setAlignment(Qt.AlignCenter)?????????? # 居中設(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 = PyQt508()
??? sys.exit(app.exec_())
?
執(zhí)行結(jié)果
