使用QSpinBox()构造计数器, 使用QSpinBox().valueChanged.connect() 来构造数值变化后的操作
QSpinBox.py
""" 计数器控件(QSpinBox) """ import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt 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_())