PyQt 5控件

python基础

浏览数:68

2019-9-2

PyQt 5控件包括:按钮、复选框、滑动条、列表框等

复选框QCheckBox

QCheckBox复选框控件,它有两个状态:打开和关闭,他是一个带有文本标签(Label)的控件。复选框常用于表示程序中可以启用或者禁用的功能。

cb = QCheckBox(‘show title’,self)

# 创建一个复选框,标题命名为show title

cb.toggle()

# 切换复选框状态,复选框默认没有勾选,并且没有标题


 1 # Author:凌逆战
 2 # -*- coding:utf-8 -*-
 3 import sys
 4 from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication
 5 from PyQt5.QtCore import Qt
 6 
 7 class Example(QWidget):
 8     def __init__(self):
 9         super().__init__()
10         self.initUI()
11 
12     def initUI(self):
13         cb = QCheckBox('Show title', self)  # 复选框自带的文本lable
14         cb.move(20, 20)
15         cb.toggle() # 切换状态# 勾选复选框
16         # 复选框stateChanged状态改变信号绑定self.changeTitle槽
17         cb.stateChanged.connect(self.changeTitle)
18 
19         self.setGeometry(300, 300, 250, 150)
20         self.setWindowTitle('QCheckBox')
21         self.show()
22 
23     def changeTitle(self, state):
24         if state == Qt.Checked:
25             self.setWindowTitle('QCheckBox')
26         else:
27             self.setWindowTitle('')
28 
29 if __name__ == '__main__':
30     app = QApplication(sys.argv)
31     ex = Example()
32     sys.exit(app.exec_())

复选框

开关按钮Toggle button

ToggleButton是QPushButton的一种特殊模式


 1 # Author:凌逆战
 2 # -*- coding:utf-8 -*-
 3 import sys
 4 from PyQt5.QtWidgets import (QWidget, QPushButton,QFrame, QApplication)
 5 from PyQt5.QtGui import QColor
 6 
 7 class Example(QWidget):
 8     def __init__(self):
 9         super().__init__()
10         self.initUI()
11 
12     def initUI(self):
13         self.col = QColor(0, 0, 0)  # 初始黑颜色的值
14 
15         redb = QPushButton('Red', self) # 创建一个红色按钮
16         redb.setCheckable(True) # 按钮设置成可检查的
17         redb.move(10, 10)
18         # 红色按钮点击信号和self.setColor槽连接在一起
19         redb.clicked[bool].connect(self.setColor)
20 
21         greenb = QPushButton('Green', self) # 创建一个绿色按钮greenb
22         greenb.setCheckable(True)   # 按钮设置成可以检查的
23         greenb.move(10, 60)
24         greenb.clicked[bool].connect(self.setColor)
25 
26         blueb = QPushButton('Blue', self)
27         blueb.setCheckable(True)    # 通过setCheckable()方法得到一个ToggleButton
28         blueb.move(10, 110)
29         blueb.clicked[bool].connect(self.setColor)
30 
31         self.square = QFrame(self)
32         self.square.setGeometry(150, 20, 100, 100)
33         self.square.setStyleSheet("QWidget { background-color: %s }" %
34                                   self.col.name())
35 
36         self.setGeometry(300, 300, 280, 170)
37         self.setWindowTitle('Toggle button')
38         self.show()
39 
40     def setColor(self, pressed):
41 
42         source = self.sender()  # 发送信号的人
43 
44         if pressed:
45             val = 255
46         else:
47             val = 0
48 
49         if source.text() == "Red":
50             self.col.setRed(val)
51         elif source.text() == "Green":
52             self.col.setGreen(val)
53         else:
54             self.col.setBlue(val)
55 
56         self.square.setStyleSheet("QFrame { background-color: %s }" %
57                                   self.col.name())
58 
59 if __name__ == '__main__':
60     app = QApplication(sys.argv)
61     ex = Example()
62     sys.exit(app.exec_())

开关按钮

滑动条QSlider

sld = QSlider(QT.Horizontal,self)

# 创建一个水平滑块


 1 # Author:凌逆战
 2 # -*- coding:utf-8 -*-
 3 import sys
 4 from PyQt5.QtWidgets import (QWidget, QSlider,QLabel, QApplication)
 5 from PyQt5.QtCore import Qt
 6 from PyQt5.QtGui import QPixmap
 7 
 8 class Example(QWidget):
 9     def __init__(self):
10         super().__init__()
11         self.initUI()
12 
13     def initUI(self):
14         sld = QSlider(Qt.Horizontal, self)  # 水平的滑动条
15         sld.setFocusPolicy(Qt.NoFocus)  # 设置焦点策略
16         sld.setGeometry(30, 40, 100, 30)
17         sld.valueChanged[int].connect(self.changeValue) # int传参
18 
19         self.label = QLabel(self)   # 创建一个QLable控件,并设置一个初始音量图标
20         self.label.setPixmap(QPixmap('audio.ico'))
21         self.label.setGeometry(160, 40, 80, 30)
22 
23         self.setGeometry(300, 300, 280, 170)
24         self.setWindowTitle('QSlider')
25         self.show()
26 
27     def changeValue(self, value):   # value形参
28 
29         if value == 0:
30             self.label.setPixmap(QPixmap('audio.ico'))  # 设置像素映射
31         elif value > 0 and value <= 30:
32             self.label.setPixmap(QPixmap('min.ico'))
33         elif value > 30 and value < 80:
34             self.label.setPixmap(QPixmap('med.ico'))
35         else:
36             self.label.setPixmap(QPixmap('max.ico'))
37 
38 if __name__ == '__main__':
39     app = QApplication(sys.argv)
40     ex = Example()
41     sys.exit(app.exec_())

滑动条

进度条QProgressBar

self.pbar = QProgressBar(self)

# 创建进度条实例


 1 # Author:凌逆战
 2 # -*- coding:utf-8 -*-
 3 import sys
 4 from PyQt5.QtWidgets import (QWidget, QProgressBar,QPushButton, QApplication)
 5 from PyQt5.QtCore import QBasicTimer
 6 
 7 class Example(QWidget):
 8     def __init__(self):
 9         super().__init__()
10         self.initUI()
11 
12     def initUI(self):
13         self.pbar = QProgressBar(self)  # 创建一个进度条QProgressBar实例
14         self.pbar.setGeometry(30, 40, 200, 25)
15 
16         self.btn = QPushButton('Start', self)   # 创建一个Start按钮实例
17         self.btn.move(40, 80)
18         self.btn.clicked.connect(self.doAction)
19 
20         self.timer = QBasicTimer()  # 创建一个定时器实例,用来激活QProgressBar
21         self.step = 0
22 
23         self.setGeometry(300, 300, 280, 170)
24         self.setWindowTitle('进度条')
25         self.show()
26     # 每一个QObject及其子类都有一个timeEvent()事件处理器,
27     # 重写timeEvrnt,实现这个事件处理器来响应定时器事件
28     def timerEvent(self, e):
29 
30         if self.step >= 100:
31             self.timer.stop()
32             self.btn.setText('完成')
33             return
34 
35         self.step = self.step + 1
36         self.pbar.setValue(self.step)   # 把step的值设置为进度条的值
37 
38     def doAction(self):
39 
40         if self.timer.isActive():
41             self.timer.stop()
42             self.btn.setText('Start')
43         else:
44             # 调用start()方法来启动一个计时器
45             # 第一个参数:超时;第二个参数:对象将接受的事件
46             self.timer.start(100, self) 
47             self.btn.setText('Stop')
48 
49 if __name__ == '__main__':
50     app = QApplication(sys.argv)
51     ex = Example()
52     sys.exit(app.exec_())

进度条

日历控件QCalendarWidget

cal = QCalendarWidget(self)

# 创建日历控件

cal.clicked[QDate].connect(self.showDate)

# 点击[QDate]发出信号,将信号clicked和self.showDate槽绑定


 1 # Author:凌逆战
 2 # -*- coding:utf-8 -*-
 3 import sys
 4 from PyQt5.QtWidgets import (QWidget, QCalendarWidget,QLabel, QApplication)
 5 from PyQt5.QtCore import QDate
 6 
 7 class Example(QWidget):
 8     def __init__(self):
 9         super().__init__()
10         self.initUI()
11 
12     def initUI(self):
13         cal = QCalendarWidget(self) # 创建一个日历控件QCalendarWidget
14         cal.setGridVisible(True)    # 设置网格可视化
15         cal.move(20, 20)
16         cal.clicked[QDate].connect(self.showDate)
17 
18         self.lbl = QLabel(self)
19         date = cal.selectedDate()
20         self.lbl.setText(date.toString())   # 将data转换成字符串
21         self.lbl.move(130, 260)
22 
23         self.setGeometry(300, 300, 350, 300)
24         self.setWindowTitle('Calendar')
25         self.show()
26 
27     def showDate(self, date):
28         self.lbl.setText(date.toString())
29 
30 if __name__ == '__main__':
31     app = QApplication(sys.argv)
32     ex = Example()
33     sys.exit(app.exec_())

日历控件

 

作者:凌逆战