QLabel、QLineEdit、QTextEdit
QLabel控件基本用法
''' QLabel控件 setAlignment():设置文本的对齐方式 setIndent():设置文本缩进 text():获取文本内容 setBuddy():设置伙伴关系 setText():设置文本内容 selectedText():返回所选择的字符 setWordWrap():设置是否允许换行 QLabel常用的信号(事件): 1. 当鼠标滑过QLabel控件时触发:linkHovered 2. 当鼠标单击QLabel控件时触发:linkActivated ''' import sys from PyQt5.QtWidgets import QVBoxLayout,QMainWindow,QApplication,QLabel,QWidget from PyQt5.QtGui import QPixmap, QPalette from PyQt5.QtCore import Qt class QLabelDemo(QWidget) : def __init__(self): super().__init__() self.initUI() def initUI(self): label1 = QLabel(self) label2 = QLabel(self) label3 = QLabel(self) label4 = QLabel(self) label1.setText("<font color=yellow>这是一个文本标签.</font>") label1.setAutoFillBackground(True) palette = QPalette() palette.setColor(QPalette.Window,Qt.blue) # 设置背景色 label1.setPalette(palette) label1.setAlignment(Qt.AlignCenter) label2.setText("<a href='#'>欢迎使用Python GUI程序</a>") label3.setAlignment(Qt.AlignCenter) label3.setToolTip('这是一个图片标签') label3.setPixmap(QPixmap("./images/python.jpg")) # 如果设为True,用浏览器打开网页,如果设为False,调用槽函数 label4.setOpenExternalLinks(True) label4.setText("<a href='https://item.jd.com/12417265.html'>感谢关注《Python从菜鸟到高手》</a>") label4.setAlignment(Qt.AlignRight) label4.setToolTip('这是一个超级链接') vbox = QVBoxLayout() vbox.addWidget(label1) vbox.addWidget(label2) vbox.addWidget(label3) vbox.addWidget(label4) label2.linkHovered.connect(self.linkHovered) label4.linkActivated.connect(self.linkClicked) self.setLayout(vbox) self.setWindowTitle('QLabel控件演示') def linkHovered(self): print('当鼠标滑过label2标签时,触发事件') def linkClicked(self): print('当鼠标单击label4标签时,触发事件') if __name__ == '__main__': app = QApplication(sys.argv) main = QLabelDemo() main.show() sys.exit(app.exec_())
QLabel控件与伙伴控件
''' QLabel与伙伴控件 mainLayout.addWidget(控件对象,rowIndex,columnIndex,row,column) ''' from PyQt5.QtWidgets import * import sys class QLabelBuddy(QDialog) : def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('QLabel与伙伴控件') nameLabel = QLabel('&Name',self) nameLineEdit = QLineEdit(self) # 设置伙伴控件 nameLabel.setBuddy(nameLineEdit) passwordLabel = QLabel('&Password',self) passwordLineEdit = QLineEdit(self) # 设置伙伴控件 passwordLabel.setBuddy(passwordLineEdit) btnOK = QPushButton('&OK') btnCancel = QPushButton('&Cancel') mainLayout = QGridLayout(self) mainLayout.addWidget(nameLabel,0,0) mainLayout.addWidget(nameLineEdit,0,1,1,2) mainLayout.addWidget(passwordLabel,1,0) mainLayout.addWidget(passwordLineEdit,1,1,1,2) mainLayout.addWidget(btnOK,2,1) mainLayout.addWidget(btnCancel,2,2) if __name__ == '__main__': app = QApplication(sys.argv) main = QLabelBuddy() main.show() sys.exit(app.exec_())
QLineEdit控件与回显模式
''' QLineEdit控件与回显模式 基本功能:输入单行的文本 EchoMode(回显模式) 4种回显模式 1. Normal 2. NoEcho 3. Password 4. PasswordEchoOnEdit Mac : Command Windows:Control ''' from PyQt5.QtWidgets import * import sys class QLineEditEchoMode(QWidget) : def __init__(self): super(QLineEditEchoMode,self).__init__() self.initUI() def initUI(self): self.setWindowTitle('文本输入框的回显模式') formLayout = QFormLayout() normalLineEdit = QLineEdit() noEchoLineEdit = QLineEdit() passwordLineEdit = QLineEdit() passwordEchoOnEditLineEdit = QLineEdit() formLayout.addRow("Normal",normalLineEdit) formLayout.addRow("NoEcho", noEchoLineEdit) formLayout.addRow("Password",passwordLineEdit) formLayout.addRow("PasswordEchoOnEdit",passwordEchoOnEditLineEdit) # placeholdertext normalLineEdit.setPlaceholderText("Normal") noEchoLineEdit.setPlaceholderText("NoEcho") passwordLineEdit.setPlaceholderText("Password") passwordEchoOnEditLineEdit.setPlaceholderText("PasswordEchoOnEdit") normalLineEdit.setEchoMode(QLineEdit.Normal) noEchoLineEdit.setEchoMode(QLineEdit.NoEcho) passwordLineEdit.setEchoMode(QLineEdit.Password) passwordEchoOnEditLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit) self.setLayout(formLayout) if __name__ == '__main__': app = QApplication(sys.argv) main = QLineEditEchoMode() main.show() sys.exit(app.exec_())
QLineEdit控件输入校验器
''' 现在QLineEdit控件的输入(校验器) 如限制只能输入整数、浮点数或满足一定条件的字符串 ''' from PyQt5.QtWidgets import * from PyQt5.QtGui import QIntValidator,QDoubleValidator, QRegExpValidator from PyQt5.QtCore import QRegExp import sys class QLineEditValidator(QWidget): def __init__(self): super(QLineEditValidator,self).__init__() self.initUI() def initUI(self): self.setWindowTitle('校验器') # 创建表单布局 formLayout = QFormLayout() intLineEdit = QLineEdit() doubleLineEdit = QLineEdit() validatorLineEdit = QLineEdit() formLayout.addRow('整数类型', intLineEdit) formLayout.addRow('浮点类型', doubleLineEdit) formLayout.addRow('数字和字母',validatorLineEdit) intLineEdit.setPlaceholderText('整型') doubleLineEdit.setPlaceholderText('浮点型') validatorLineEdit.setPlaceholderText('字母和数字') # 整数校验器 [1,99] intValidator = QIntValidator(self) intValidator.setRange(1,99) # 浮点校验器 [-360,360],精度:小数点后2位 doubleValidator = QDoubleValidator(self) doubleValidator.setRange(-360,360) doubleValidator.setNotation(QDoubleValidator.StandardNotation) # 设置精度,小数点2位 doubleValidator.setDecimals(2) # 字符和数字 reg = QRegExp('[a-zA-z0-9]+$') validator = QRegExpValidator(self) validator.setRegExp(reg) # 设置校验器 intLineEdit.setValidator(intValidator) doubleLineEdit.setValidator(doubleValidator) validatorLineEdit.setValidator(validator) self.setLayout(formLayout) if __name__ == '__main__': app = QApplication(sys.argv) main = QLineEditValidator() main.show() sys.exit(app.exec_())
QLineEdit掩码限制输入
''' 用掩码限制QLineEdit控件的输入 A ASCII字母字符是必须输入的(A-Z、a-z) a ASCII字母字符是允许输入的,但不是必需的(A-Z、a-z) N ASCII字母字符是必须输入的(A-Z、a-z、0-9) n ASII字母字符是允许输入的,但不是必需的(A-Z、a-z、0-9) X 任何字符都是必须输入的 x 任何字符都是允许输入的,但不是必需的 9 ASCII数字字符是必须输入的(0-9) 0 ASCII数字字符是允许输入的,但不是必需的(0-9) D ASCII数字字符是必须输入的(1-9) d ASCII数字字符是允许输入的,但不是必需的(1-9) # ASCI数字字符或加减符号是允许输入的,但不是必需的 H 十六进制格式字符是必须输入的(A-F、a-f、0-9) h 十六进制格式字符是允许输入的,但不是必需的(A-F、a-f、0-9) B 二进制格式字符是必须输入的(0,1) b 二进制格式字符是允许输入的,但不是必需的(0,1) > 所有的字母字符都大写 < 所有的字母字符都小写 ! 关闭大小写转换 \ 使用"\"转义上面列出的字符 ''' from PyQt5.QtWidgets import * import sys class QLineEditMask(QWidget) : def __init__(self): super(QLineEditMask,self).__init__() self.initUI() def initUI(self): self.setWindowTitle('用掩码限制QLineEdit控件的输入') formLayout = QFormLayout() ipLineEdit = QLineEdit() macLineEdit = QLineEdit() dateLineEdit = QLineEdit() licenseLineEdit = QLineEdit() # 192.168.21.45 ipLineEdit.setInputMask('000.000.000.000;_') macLineEdit.setInputMask('HH:HH:HH:HH:HH:HH;_') dateLineEdit.setInputMask('0000-00-00') licenseLineEdit.setInputMask('>AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#') formLayout.addRow('数字掩码',ipLineEdit) formLayout.addRow('Mac掩码', macLineEdit) formLayout.addRow('日期掩码',dateLineEdit) formLayout.addRow('许可证掩码',licenseLineEdit) self.setLayout(formLayout) if __name__ == '__main__': app = QApplication(sys.argv) main = QLineEditMask() main.show() sys.exit(app.exec_())
QLineEdit综合案例
''' QLineEdit综合案例 ''' from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import Qt import sys class QLineEditDemo(QWidget): def __init__(self): super(QLineEditDemo,self).__init__() self.initUI() def initUI(self): edit1 = QLineEdit() # 使用int校验器 edit1.setValidator(QIntValidator()) edit1.setMaxLength(4) # 不超过9999 edit1.setAlignment(Qt.AlignRight) edit1.setFont(QFont('Arial',20)) edit2 = QLineEdit() edit2.setValidator(QDoubleValidator(0.99,99.99,2)) edit3 = QLineEdit() edit3.setInputMask('99_9999_999999;#') edit4 = QLineEdit() edit4.textChanged.connect(self.textChanged) edit5 = QLineEdit() edit5.setEchoMode(QLineEdit.Password) edit5.editingFinished.connect(self.enterPress) edit6 = QLineEdit('Hello PyQt5') edit6.setReadOnly(True) formLayout = QFormLayout() formLayout.addRow('整数校验',edit1) formLayout.addRow('浮点数校验',edit2) formLayout.addRow('Input Mask',edit3) formLayout.addRow('文本变化', edit4) formLayout.addRow('密码',edit5) formLayout.addRow('只读',edit6) self.setLayout(formLayout) self.setWindowTitle('QLineEdit综合案例') def textChanged(self,text): print('输入的内容:' + text) def enterPress(self): print('已输入值') if __name__ == '__main__': app = QApplication(sys.argv) main = QLineEditDemo() main.show() sys.exit(app.exec_())
QTextEdit输入多行文本
''' QTextEdit控件 ''' from PyQt5.QtWidgets import * import sys class QTextEditDemo(QWidget) : def __init__(self): super(QTextEditDemo,self).__init__() self.initUI() def initUI(self): self.setWindowTitle('QTextEdit控件演示') self.resize(300,320) self.textEdit = QTextEdit() self.buttonText = QPushButton('显示文本') self.buttonHTML = QPushButton('显示HTML') self.buttonToText = QPushButton('获取文本') self.buttonToHTML = QPushButton('获取HTML') layout = QVBoxLayout() layout.addWidget(self.textEdit) layout.addWidget(self.buttonText) layout.addWidget(self.buttonToText) layout.addWidget(self.buttonHTML) layout.addWidget(self.buttonToHTML) self.setLayout(layout) self.buttonText.clicked.connect(self.onClick_ButtonText) self.buttonHTML.clicked.connect(self.onClick_ButtonHTML) self.buttonToText.clicked.connect(self.onClick_ButtonToText) self.buttonToHTML.clicked.connect(self.onClick_ButtonToHTML) def onClick_ButtonText(self): self.textEdit.setPlainText('Hello World,世界你好吗?') def onClick_ButtonToText(self): print(self.textEdit.toPlainText()) def onClick_ButtonHTML(self): self.textEdit.setHtml('<font color="blue" size="5">Hello World</font>') def onClick_ButtonToHTML(self): print(self.textEdit.toHtml()) if __name__ == '__main__': app = QApplication(sys.argv) main = QTextEditDemo() main.show() sys.exit(app.exec_())
天道酬勤 循序渐进 技压群雄