PyQt5窗口设计基础之常用控件类(二)
一、文本类控件
1. Label: 标签控件
2. LineEdit: 单行文本框
3. TextEdit: 多行文本控件
主要用来显示多行的文本内容
当文本内容超出控件的显示区域时,将会显示垂直滚动条
除显示纯文本内容外,还支持显示HTML网页
Qt有
Plain TextEdit
控件以显示纯文本
self.teditPlain.setPlainText('这是一个喜欢宁宁的纯文本')
self.teditHTML.setHtml('这是一个<font color="red" size =12>喜欢宁宁</font>的HTML文本')
strPlain = self.teditHTML.toPlainText()
strHtml = self.teditHTML.toHtml()
print('纯文本显示如下:')
print(strPlain)
print('\n html文本显示如下:')
print(strHtml)
html文本显示如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">这是一个<span style=" color:#ff0000;">喜欢宁宁</span>的HTML文本</p></body></html>
QTextEdit类的常用方法及说明
self.doubleSpinBox.setRange(0, 10)
self.doubleSpinBox.setSingleStep(0.5)
self.doubleSpinBox.setDecimals(3)
self.spinBox.valueChanged.connect(self.showSpinBoxNumbers)
self.doubleSpinBox.valueChanged.connect(self.showSpinBoxNumbers)
def showSpinBoxNumbers(self):
self.label.setText('整数控件数字为:' + str(self.spinBox.value()) + ' '
'小数控件数字为:' + str(self.doubleSpinBox.value()))
首先自定义显示俩控件数值的一个槽函数howSpinBoxNumbers()
再将spinBox与doubleSpinBox的valueChanged信号与自定义槽函数相连
注意:value()方法返回的类型为数字类型,需要str()后才能显示
from PyQt5.QtGui import QRegExpValidator, QIntValidator
from PyQt5.QtCore import QRegExp
reg = QRegExp('^[0-9]*$')
regExpValidator = QRegExpValidator(None)
regExpValidator.setRegExp(reg)
self.lineEdit.setValidator(regExpValidator)
self.lineEdit.editingFinished.connect(self.getValue)
def getValue(self):
lcdNumber = self.lineEdit.text()
from PyQt5.QtWidgets import QLCDNumber
self.lcnBin.setDigitCount(10)
self.lcnBin.setProperty('value', lcdNumber)
self.lcnBin.setMode(QLCDNumber.Bin)
self.lcnOct.setDigitCount(10)
self.lcnOct.setProperty('value', lcdNumber)
self.lcnOct.setMode(QLCDNumber.Oct)
self.lcnDec.setDigitCount(10)
self.lcnDec.setProperty('value', lcdNumber)
self.lcnDec.setMode(QLCDNumber.Dec)
self.lcnHex.setDigitCount(10)
self.lcnHex.setProperty('value', lcdNumber)
self.lcnHex.setMode(QLCDNumber.Hex)
关于setSegmentStyle()方法虽然只能接受三种风格,但是,别忘记还有setStyleSheet()这个方法
也可以结合time类,使用QLCDNumber类display(self, str)方法做成时钟
from PyQt5.QtCore import QTimer
import time
class Ui_Form(object):
def setupUi(self, Form):
/* 自动生成的代码省略*/
self.lcdNumber.setDigitCount(20)
timer = QTimer()
timer.start()
timer.timeout.connect(self.clock)
self.timer = timer
def clock(self):
# print('signal')
t = time.strftime('%Y-%m-%d %H:%M:%S')
print(t)
self.lcdNumber.display(t)