第四课 PyQt5按钮、滑块、计数器控件
tags:
- python
- 网易云课堂
categories:
- pyqt5
- 按钮控件
- 滑块控件
- 计数器控件
文章目录
第一节 按钮控件
1. 按钮控件分类
- 按钮控件的基类QAbstractButton
- QPushButton(普通按钮)
- AToolButton(工具条按钮)
- QRadioButton(单选按钮)
- QCheckBox(多选按钮)
2. QPushButton按钮控件案例
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QPushButtonDemo(QDialog):
def __init__(self):
super(QPushButtonDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QPushButton Demo')
layout = QVBoxLayout()
self.button1 = QPushButton('第1个按钮')
self.button1.setText('First Button1')
# 可复选
self.button1.setCheckable(True)
self.button1.toggle()
self.button1.clicked.connect(self.buttonState)
# 如果用上面的方法,不能给绑定的函数传参数。可以用lambda调用传参
self.button1.clicked.connect(lambda: self.whichButton(self.button1))
layout.addWidget(self.button1)
# 在文本前面显示图像
self.button2 = QPushButton('图像按钮')
self.button2.setIcon(QIcon(QPixmap('./images/python.png')))
self.button2.clicked.connect(lambda: self.whichButton(self.button2))
layout.addWidget(self.button2)
self.button3 = QPushButton('不可用的按钮')
self.button3.setEnabled(False)
layout.addWidget(self.button3)
# 热键只在win下可用,mac下不支持热键
self.button4 = QPushButton('&MyButton')
self.button4.setDefault(True)
self.button4.clicked.connect(lambda: self.whichButton(self.button4))
layout.addWidget(self.button4)
self.setLayout(layout)
self.resize(400, 300)
def buttonState(self):
if self.button1.isChecked():
print('按钮1已经被选中')
else:
print('按钮1未被选中')
def whichButton(self, btn):
print('被单击的按钮是<' + btn.text() + '>')
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QPushButtonDemo()
main.show()
sys.exit(app.exec_())
3. QRadioButton单选按钮控件案例
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QRadioButtonDemo(QWidget):
def __init__(self):
super(QRadioButtonDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QRadioButton')
layout = QHBoxLayout()
self.button1 = QRadioButton('单选按钮1')
self.button1.setChecked(True)
self.button1.toggled.connect(self.buttonState)
layout.addWidget(self.button1)
self.button2 = QRadioButton('单选按钮2')
self.button2.toggled.connect(self.buttonState)
layout.addWidget(self.button2)
self.setLayout(layout)
def buttonState(self):
radioButton = self.sender()
if radioButton.isChecked() == True:
print('<' + radioButton.text() + '> 被选中')
else:
print('<' + radioButton.text() + '> 被取消选中状态')
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QRadioButtonDemo()
main.show()
sys.exit(app.exec_())
3. QCheckBox复选框控件案例
- 复选框控件(QCheckBox)有3种状态:未选中:0、半选中:1、选中:2
- 这里setTristate(True)只有设为真才有可能处于半选中状态
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
class QCheckBoxDemo(QWidget):
def __init__(self):
super(QCheckBoxDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('复选框控件演示')
layout = QHBoxLayout()
self.checkBox1 = QCheckBox('复选框控件1')
self.checkBox1.setChecked(True)
self.checkBox1.stateChanged.connect(lambda: self.checkboxState(self.checkBox1))
layout.addWidget(self.checkBox1)
self.checkBox2 = QCheckBox('复选框控件2')
self.checkBox2.stateChanged.connect(lambda: self.checkboxState(self.checkBox2))
layout.addWidget(self.checkBox2)
self.checkBox3 = QCheckBox('半选中')
self.checkBox3.stateChanged.connect(lambda: self.checkboxState(self.checkBox3))
# 这里只有设为真才有可能处于半选中状态
self.checkBox3.setTristate(True)
self.checkBox3.setCheckState(Qt.PartiallyChecked)
layout.addWidget(self.checkBox3)
self.setLayout(layout)
def checkboxState(self, cb):
check1Status = self.checkBox1.text() + ', isChecked=' + str(self.checkBox1.isChecked()) + ',checkState=' + str(
self.checkBox1.checkState()) + '\n'
check2Status = self.checkBox2.text() + ', isChecked=' + str(self.checkBox2.isChecked()) + ',checkState=' + str(
self.checkBox2.checkState()) + '\n'
check3Status = self.checkBox3.text() + ', isChecked=' + str(self.checkBox3.isChecked()) + ',checkState=' + str(
self.checkBox3.checkState()) + '\n'
print(check1Status + check2Status + check3Status)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QCheckBoxDemo()
main.show()
sys.exit(app.exec_())
4.下拉列表控件(QComboBox)
- 如何将列表项添加到QComboBox控件中
- 如何获取选中的列表项
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QComboBoxDemo(QWidget):
def __init__(self):
super(QComboBoxDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('下拉列表控件演示')
self.resize(300, 100)
layout = QVBoxLayout()
self.label = QLabel('请选择编程语言')
self.cb = QComboBox()
self.cb.addItem('C++')
self.cb.addItem('Python')
# 添加多个
self.cb.addItems(['Java', 'C#', 'Ruby'])
self.cb.currentIndexChanged.connect(self.selectionChange)
layout.addWidget(self.label)
layout.addWidget(self.cb)
self.setLayout(layout)
def selectionChange(self, i):
self.label.setText(self.cb.currentText())
self.label.adjustSize()
for count in range(self.cb.count()):
print('item' + str(count) + '=' + self.cb.itemText(count))
print('current index', i, 'selection changed', self.cb.currentText())
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QComboBoxDemo()
main.show()
sys.exit(app.exec_())
第二节 滑块控件QSlider
- 使用滑块控件设置字体大小。
'''
滑块控件(QSlider)
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QSliderDemo(QWidget):
def __init__(self):
super(QSliderDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('滑块控件演示')
self.resize(300, 700)
layout = QVBoxLayout()
self.label = QLabel('你好 PyQt5')
self.label.setAlignment(Qt.AlignCenter)
layout.addWidget(self.label)
self.slider = QSlider(Qt.Horizontal)
# 设置最小值
self.slider.setMinimum(12)
# 设置最大值
self.slider.setMaximum(48)
# 步长
self.slider.setSingleStep(3)
# 设置当前值
self.slider.setValue(18)
# 设置刻度的位置,刻度在下方
self.slider.setTickPosition(QSlider.TicksBelow)
# 设置刻度的间隔
self.slider.setTickInterval(6)
layout.addWidget(self.slider)
self.slider.valueChanged.connect(self.valueChange)
self.slider1 = QSlider(Qt.Vertical)
layout.addWidget(self.slider1)
# 设置最小值
self.slider1.setMinimum(10)
# 设置最大值
self.slider1.setMaximum(60)
# 步长
self.slider1.setSingleStep(5)
# 设置当前值
self.slider1.setValue(30)
# 设置刻度的位置,刻度在下方
self.slider1.setTickPosition(QSlider.TicksLeft)
# 设置刻度的间隔
self.slider1.setTickInterval(2)
self.slider1.valueChanged.connect(self.valueChange)
self.setLayout(layout)
def valueChange(self):
print('当前值:%s' % self.sender().value())
size = self.sender().value()
self.label.setFont(QFont('Arial', size))
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QSliderDemo()
main.show()
sys.exit(app.exec_())
第三节 计数器控件QSpinBox
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QSpinBoxDemo(QWidget):
def __init__(self):
super(QSpinBoxDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QSpinBox演示')
self.resize(300, 100)
layout = QVBoxLayout()
self.label = QLabel('当前值')
self.label.setAlignment(Qt.AlignCenter)
layout.addWidget(self.label)
self.sb = QSpinBox()
self.sb.setValue(18)
self.sb.setRange(10, 38)
self.sb.setSingleStep(3)
layout.addWidget(self.sb)
self.sb.valueChanged.connect(self.valueChange)
self.setLayout(layout)
def valueChange(self):
self.label.setText('当前值:' + str(self.sb.value()))
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QSpinBoxDemo()
main.show()
sys.exit(app.exec_())