下面的例子主要是使用滑动窗口来控制字体的大小, 字体大小的设置通过QLabel().setFont(QFont('Arial', size))
QSliderDemo.py
""" 滑块控件(QSlider) """ import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt class SliderDemo(QWidget): def __init__(self): super(SliderDemo, 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) self.slider.valueChanged.connect(self.valueChange) self.slider1 = QSlider(Qt.Vertical) # 设置最小值 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) layout.addWidget(self.slider) layout.addWidget(self.slider1) self.setLayout(layout) def valueChange(self): print("当前值: %s" % self.slider.value()) size = self.sender().value() self.label.setFont(QFont('Arial', size)) if __name__ == "__main__": app = QApplication(sys.argv) main = SliderDemo() main.show() sys.exit(app.exec_())