PyQt 5控件包括:按钮、复选框、滑动条、列表框等
复选框QCheckBox
QCheckBox复选框控件,它有两个状态:打开和关闭,他是一个带有文本标签(Label)的控件。复选框常用于表示程序中可以启用或者禁用的功能。
cb = QCheckBox('show title',self)
# 创建一个复选框,标题命名为show title
cb.toggle()
# 切换复选框状态,复选框默认没有勾选,并且没有标题
# Author:凌逆战
# -*- coding:utf-8 -*-
import sys
from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication
from PyQt5.QtCore import Qt class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI() def initUI(self):
cb = QCheckBox('Show title', self) # 复选框自带的文本lable
cb.move(20, 20)
cb.toggle() # 切换状态# 勾选复选框
# 复选框stateChanged状态改变信号绑定self.changeTitle槽
cb.stateChanged.connect(self.changeTitle) self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('QCheckBox')
self.show() def changeTitle(self, state):
if state == Qt.Checked:
self.setWindowTitle('QCheckBox')
else:
self.setWindowTitle('') if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
复选框
开关按钮Toggle button
ToggleButton是QPushButton的一种特殊模式
# Author:凌逆战
# -*- coding:utf-8 -*-
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton,QFrame, QApplication)
from PyQt5.QtGui import QColor class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI() def initUI(self):
self.col = QColor(0, 0, 0) # 初始黑颜色的值 redb = QPushButton('Red', self) # 创建一个红色按钮
redb.setCheckable(True) # 按钮设置成可检查的
redb.move(10, 10)
# 红色按钮点击信号和self.setColor槽连接在一起
redb.clicked[bool].connect(self.setColor) greenb = QPushButton('Green', self) # 创建一个绿色按钮greenb
greenb.setCheckable(True) # 按钮设置成可以检查的
greenb.move(10, 60)
greenb.clicked[bool].connect(self.setColor) blueb = QPushButton('Blue', self)
blueb.setCheckable(True) # 通过setCheckable()方法得到一个ToggleButton
blueb.move(10, 110)
blueb.clicked[bool].connect(self.setColor) self.square = QFrame(self)
self.square.setGeometry(150, 20, 100, 100)
self.square.setStyleSheet("QWidget { background-color: %s }" %
self.col.name()) self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('Toggle button')
self.show() def setColor(self, pressed): source = self.sender() # 发送信号的人 if pressed:
val = 255
else:
val = 0 if source.text() == "Red":
self.col.setRed(val)
elif source.text() == "Green":
self.col.setGreen(val)
else:
self.col.setBlue(val) self.square.setStyleSheet("QFrame { background-color: %s }" %
self.col.name()) if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
开关按钮
滑动条QSlider
sld = QSlider(QT.Horizontal,self)
# 创建一个水平滑块
# Author:凌逆战
# -*- coding:utf-8 -*-
import sys
from PyQt5.QtWidgets import (QWidget, QSlider,QLabel, QApplication)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI() def initUI(self):
sld = QSlider(Qt.Horizontal, self) # 水平的滑动条
sld.setFocusPolicy(Qt.NoFocus) # 设置焦点策略
sld.setGeometry(30, 40, 100, 30)
sld.valueChanged[int].connect(self.changeValue) # int传参 self.label = QLabel(self) # 创建一个QLable控件,并设置一个初始音量图标
self.label.setPixmap(QPixmap('audio.ico'))
self.label.setGeometry(160, 40, 80, 30) self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('QSlider')
self.show() def changeValue(self, value): # value形参 if value == 0:
self.label.setPixmap(QPixmap('audio.ico')) # 设置像素映射
elif value > 0 and value <= 30:
self.label.setPixmap(QPixmap('min.ico'))
elif value > 30 and value < 80:
self.label.setPixmap(QPixmap('med.ico'))
else:
self.label.setPixmap(QPixmap('max.ico')) if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
滑动条
进度条QProgressBar
self.pbar = QProgressBar(self)
# 创建进度条实例
# Author:凌逆战
# -*- coding:utf-8 -*-
import sys
from PyQt5.QtWidgets import (QWidget, QProgressBar,QPushButton, QApplication)
from PyQt5.QtCore import QBasicTimer class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI() def initUI(self):
self.pbar = QProgressBar(self) # 创建一个进度条QProgressBar实例
self.pbar.setGeometry(30, 40, 200, 25) self.btn = QPushButton('Start', self) # 创建一个Start按钮实例
self.btn.move(40, 80)
self.btn.clicked.connect(self.doAction) self.timer = QBasicTimer() # 创建一个定时器实例,用来激活QProgressBar
self.step = 0 self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('进度条')
self.show()
# 每一个QObject及其子类都有一个timeEvent()事件处理器,
# 重写timeEvrnt,实现这个事件处理器来响应定时器事件
def timerEvent(self, e): if self.step >= 100:
self.timer.stop()
self.btn.setText('完成')
return self.step = self.step + 1
self.pbar.setValue(self.step) # 把step的值设置为进度条的值 def doAction(self): if self.timer.isActive():
self.timer.stop()
self.btn.setText('Start')
else:
# 调用start()方法来启动一个计时器
# 第一个参数:超时;第二个参数:对象将接受的事件
self.timer.start(100, self)
self.btn.setText('Stop') if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
进度条
日历控件QCalendarWidget
cal = QCalendarWidget(self)
# 创建日历控件
cal.clicked[QDate].connect(self.showDate)
# 点击[QDate]发出信号,将信号clicked和self.showDate槽绑定
# Author:凌逆战
# -*- coding:utf-8 -*-
import sys
from PyQt5.QtWidgets import (QWidget, QCalendarWidget,QLabel, QApplication)
from PyQt5.QtCore import QDate class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI() def initUI(self):
cal = QCalendarWidget(self) # 创建一个日历控件QCalendarWidget
cal.setGridVisible(True) # 设置网格可视化
cal.move(20, 20)
cal.clicked[QDate].connect(self.showDate) self.lbl = QLabel(self)
date = cal.selectedDate()
self.lbl.setText(date.toString()) # 将data转换成字符串
self.lbl.move(130, 260) self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('Calendar')
self.show() def showDate(self, date):
self.lbl.setText(date.toString()) if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
日历控件