PyQt控件介绍
- 一、常用组件介绍
- 1、综合示例
- 2、讲解
- 二、深入研究组件
- 1、标签
- 1.示例代码
- 2.讲解
- 2、文本编辑框
- 1.示例
- 2.讲解
本节介绍的组件在前面的讲解过程中已经使用过了,这里综合前面的布局和信号,做一个统一的示例讲解,并对其中重点组件讲解更复杂的属性配置。
一、常用组件介绍
控件就像是应用这座房子的一块块砖。PyQt5有很多的控件,比如按钮,单选框,滑动条,复选框等等。
在本节,将介绍一些很有用的控件:QCheckBox,ToggleButton,QSlider,QLabel,QProgressBar和QCalendarWidget,QLineEdit等。
1、综合示例
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtGui import *
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def keyPressEvent(self,e):
if e.key() == Qt.Key_Q:
self.close()
def initUI(self):
# 放置组件
self.price_lb = QLabel("<b>Pricepal:</b>",self)
self.rate_lb = QLabel("Rate:",self)
self.year_lb = QLabel("Years:",self)
self.amount_lb = QLabel("Amount:",self)
self.result_bl = QLabel(self)
self.price_box = QDoubleSpinBox(self)
self.price_box.setPrefix("¥")
self.price_box.setRange(0,1000000000)
self.rate_box = QDoubleSpinBox(self)
self.rate_box.setSuffix("%")
# self.rate_box.setAlignment(Qt.AlignRight|Qt.AlignVCenter)# 设置框中的文字对其方式,默认左对齐
self.rate_box.setRange(0,100)
self.year_cob = QComboBox(self)
self.year_cob.addItems(["1年","2年","3年","4年"])
#
self.button = QPushButton("隐藏日历",self)
self.cal = QCalendarWidget(self)
self.cal.setGridVisible(True)
self.pro = QProgressBar(self)
self.pro.setValue(50)
self.welcome = QLabel(self)
movie = QMovie("welcome.gif")
self.welcome.setMovie(movie)
movie.start()
# 布局
grid = QGridLayout()
grid.addWidget(self.price_lb,0,0)
grid.addWidget(self.price_box,0,1)
grid.addWidget(self.rate_lb,1,0)
grid.addWidget(self.rate_box,1,1)
grid.addWidget(self.year_lb,2,0)
grid.addWidget(self.year_cob,2,1)
grid.addWidget(self.amount_lb,3,0)
grid.addWidget(self.result_bl,3,1)
# 纵向盒布局
vlayout = QVBoxLayout()
vlayout.addWidget(self.welcome)
vlayout.addLayout(grid)
vlayout.addWidget(self.button)
vlayout.addWidget(self.cal)
vlayout.addWidget(self.pro)
self.setLayout(vlayout)
# 绑定信号
self.price_box.valueChanged.connect(self.update)
self.rate_box.valueChanged.connect(self.update)
self.year_cob.currentIndexChanged.connect(self.update)
self.button.clicked.connect(lambda:self.cal.close())
# 窗口设置
self.setGeometry(300,300,800,300)
self.setWindowTitle("综合示例")
# 自定义信号槽(函数)
def update(self):
price = self.price_box.value()
rate = self.rate_box.value()
year = self.year_cob.currentIndex()
self.result_bl.setText("<font color=red>%s%s</font>"%("¥ ",str(round(price*((1+(rate/100.00))**(year+1)),2))))
if __name__ == "__main__":
app = QApplication([])
ex = Example()
ex.show()
app.exec_()
2、讲解
self.price_box = QDoubleSpinBox(self)
self.price_box.setPrefix("¥")
self.price_box.setRange(0,1000000000)
实例化双精度的SpinBox组件,默认小数位2位,可以通过属性setDecimals()来改变,然后两行代码设置了该组件的前缀符号,并设置了数字范围。
self.rate_box.setSuffix("%")
设置后缀符号
self.year_cob = QComboBox(self)
self.year_cob.addItems(["1年","2年","3年","4年"])
实例化了下拉框组件,并给它添加了下拉框内容,需要说明的是,该组件可以采用addItems属性一次增加多个内容,也可以使用addItem一次增加一个内容,当然两个可以混合使用(先增加多个,再增加单个)。
self.cal = QCalendarWidget(self)
self.cal.setGridVisible(True)
实例化了日历组件,并使用setGridVisible属性使得组件显示网格格式。
self.pro = QProgressBar(self)
self.pro.setValue(50)
实例化了进度条,并使用setValue属性设置了其初始化的值,当然实际应用中初始化的值一般都是0,这里只是为了方便观察。
self.welcome = QLabel(self)
movie = QMovie("welcome.gif")
self.welcome.setMovie(movie)
movie.start()
实例化了标签组件,QMovie(“welcome.gif”)加载一个动态图,并使用标签组件的setMovie属性设置标签的显示是动态图,最后使用movie.start()使得动态图循环播放。其实标签的设置属性很多,就显示而言,可以是纯字符串(setText()),也可以是图片(setPixmap()),还可以是动态图,网址等等,这个后面细说。
另外要说明的是,图片,movie等组件导入方法是from PyQt5.QtGui import QPixmap,QPalette,QMovie。
# 绑定信号
self.price_box.valueChanged.connect(self.update)
self.rate_box.valueChanged.connect(self.update)
self.year_cob.currentIndexChanged.connect(self.update)
self.button.clicked.connect(lambda:self.cal.close())
绑定信号代码中,其他的前面讲解都有使用,而下拉框组件的currentIndexChanged属性就是当前索引改变时触发,它还有currentTextChanged,即当前值改变时触发。
二、深入研究组件
1、标签
标签组件作为一个占位符,可以什么都不显示,只占用窗口的位置,也可以显示文本、图片、动画,甚至可以是一个外部链接。
1.示例代码
from PyQt5.QtWidgets import QApplication,QLabel,QWidget,QVBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap,QPalette,QMovie
class Example(QWidget):
def __init__(self):
super().__init__()
self.updateUI()
def updateUI(self):
lb1 = QLabel(self)
lb2 = QLabel(self)
lb3 = QLabel(self)
# 初始化标签
lb1.setText("这是一个文本标签")
lb1.setAutoFillBackground(True)
palette = QPalette()
palette.setColor(QPalette.Window,Qt.green)
lb1.setPalette(palette)
lb1.setAlignment(Qt.AlignCenter)
lb2.setText("<a href='https://www.baidu.com/'>百度一下</a>")
lb2.setOpenExternalLinks(True)
movie = QMovie(r"E:\PyQt界面\ico\下载动图.gif")# 加载动图
lb3.setMovie(movie)# 设置标签3显示动图
movie.start()# 动图开始显示
# 布局
vbox = QVBoxLayout()
vbox.addWidget(lb1)
vbox.addStretch()
vbox.addWidget(lb2)
vbox.addStretch()
vbox.addWidget(lb3)
vbox.addStretch()
self.setLayout(vbox)
# 窗口属性设置
self.setGeometry(300,300,400,300)
self.setWindowTitle("标签的各种属性研究")
if __name__ == "__main__":
app = QApplication([])
ex = Example()
ex.show()
app.exec_()
2.讲解
lb1.setAutoFillBackground(True)
lb1.setAlignment(Qt.AlignCenter)
设置背景可以设置填充并设置标签字体格式居中。
palette = QPalette()
palette.setColor(QPalette.Window,Qt.green)
lb1.setPalette(palette)
调用Qt的调色板,并设置调色板颜色为绿色,最后通过标签的setPalette属性设置背景填充颜色。
lb2.setText("<a href='https://www.baidu.com/'>百度一下</a>")
lb2.setOpenExternalLinks(True)# 开启访问外部链接权限
2、文本编辑框
1.示例
from PyQt5.QtWidgets import QApplication,QLineEdit,QFormLayout,QWidget
from PyQt5.QtGui import QIntValidator,QDoubleValidator,QFont
from PyQt5.QtCore import Qt
import sys
class lineEditDemo(QWidget):
def __init__(self,parent=None):
super(lineEditDemo, self).__init__(parent)
e1=QLineEdit()
e1.setValidator(QIntValidator())
e1.setMaxLength(4)
e1.setAlignment(Qt.AlignRight)
e1.setFont(QFont('Arial',20))
e1.setPlaceholderText('整型数字')
#创建文本
e2=QLineEdit()
e2.setValidator(QDoubleValidator(0.99,99.99,2))
e2.setPlaceholderText('双精度数字')
#表单布局
flo=QFormLayout()
#添加名称及控件到布局中
flo.addRow('integer validator',e1)
flo.addRow('Double Validator',e2)
#创建文本
e3=QLineEdit()
e3.setInputMask('+99_9999_999999')
flo.addRow('Input Mask',e3)
e4=QLineEdit()
#文本修改信号发射与槽函数的绑定
e4.textChanged.connect(self.textchanged)
flo.addRow('Text changed',e4)
e5=QLineEdit()
e5.setEchoMode(QLineEdit.Password)
flo.addRow('Password',e5)
#创建文本框并增添文本框的内容
e6=QLineEdit('HELLO PyQt5')
#设置属性为只读
e6.setReadOnly(True)
flo.addRow('Read Only',e6)
#编译完成的信号与槽函数的绑定
e5.editingFinished.connect(self.enterPress)
#设置窗口的布局
self.setLayout(flo)
self.setWindowTitle("QLinedit例子")
def textchanged(self,text):
print('输入的内容为'+text)
def enterPress(self):
print('已输入')
if __name__ == '__main__':
app=QApplication(sys.argv)
win=lineEditDemo()
win.show()
app.exec_()
2.讲解
e1=QLineEdit()
#设置文本校验器为整数,只有输入整数才为有效值
e1.setValidator(QIntValidator())
#设置允许输入的最大字符数
e1.setMaxLength(4)
#设置文本靠右对齐
e1.setAlignment(Qt.AlignRight)
#设置文本的字体和字号大小
e1.setFont(QFont('Arial',20))
#设置文本框的默认浮现文本
e1.setPlaceholderText('整型数字')
e2.setValidator(QDoubleValidator(0.99,99.99,2))
设置浮点型校验器,有效范围(0.99-99.99),保留两位小数
e3.setInputMask('+99_9999_999999')
定义文本输入掩码,ASCII字母字符是必须输入的(0-9)
e5=QLineEdit()
e5.setEchoMode(QLineEdit.Password)
设置文本框显示的格式,QLineEdit.Password:显示密码掩码字符,而不是实际输入的字符。
讲解到这里,估计对PyQt的控件有了一定的理解,持续更新中…
👊🙈 …