目录
1、滑动条QSlider
1)常用属性
self.slider = QSlider(Qt.Horizontal) # 设置最小值 self.slider.setMinimum(2) # 设置最大值 self.slider.setMaximum(48) # 设置步长 self.slider.setSingleStep(1) # 设置当前值 self.slider.setValue(2) # 设置刻度 self.slider.setTickPosition(QSlider.TicksBelow) # 下方 # 设置刻度显示间隔 self.slider.setTickInterval(1) # 值变化信号 self.slider.valueChanged.connect(self.valueChange)
2)实例——利用滑动条来实现字体大小的修改
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/5/25 17:44
# @Author : @linlianqin
# @Site :
# @File : QSlider_learn.py
# @Software: PyCharm
# @description:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class QsliderDemo(QWidget):
def __init__(self):
super(QsliderDemo, self).__init__()
self.InitUI()
def InitUI(self):
self.setWindowTitle("滑块学习")
self.resize(500,300)
layout = QVBoxLayout()
self.label = QLabel("Hello World")
self.slider = QSlider(Qt.Horizontal)
# 设置最小值
self.slider.setMinimum(2)
# 设置最大值
self.slider.setMaximum(48)
# 设置步长
self.slider.setSingleStep(1)
# 设置当前值
self.slider.setValue(2)
# 设置刻度
self.slider.setTickPosition(QSlider.TicksBelow) # 下方
# 设置刻度显示间隔
self.slider.setTickInterval(1)
# 值变化信号
self.slider.valueChanged.connect(self.valueChange)
layout.addWidget(self.label)
layout.addWidget(self.slider)
self.setLayout(layout)
#
def valueChange(self):
print("当前值:",self.slider.value())
size = self.slider.value()
self.label.setFont(QFont("Arial",size))
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = QsliderDemo()
mainWin.show()
sys.exit(app.exec_())
2、计数器QSpinBox
1)属性方法
2)实例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/5/25 18:01
# @Author : @linlianqin
# @Site :
# @File : QSpinBox_learn.py
# @Software: PyCharm
# @description:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class QSpinBoxDemo(QWidget):
def __init__(self):
super(QSpinBoxDemo, self).__init__()
self.InitUI()
def InitUI(self):
self.setWindowTitle("计数器控件学习")
self.resize(500,100)
self.label = QLabel("当前值")
self.label.setAlignment(Qt.AlignCenter)
layout = QVBoxLayout()
layout.addWidget(self.label)
self.spinbox = QSpinBox()
layout.addWidget(self.spinbox)
self.spinbox.valueChanged.connect(self.valuechange)
self.setLayout(layout)
def valuechange(self):
self.label.setText("当前值:"+str(self.spinbox.value()))
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = QSpinBoxDemo()
mainWin.show()
sys.exit(app.exec_())