锋哥原创的PyQt6视频教程:
QSpinBox是一个整数数字选择控件,该控件提供一对上下箭头,用户可以单击上下箭头选择数值,也可以直接输入。如果输入的数值大于设置的最大值,或者小于设置的最小值,SpinBox将不会接受输入。
SpinBox类的常用方法如下:
方法 | 描述 |
---|---|
setValue() | 设置控件的当前值 |
setMaximum() | 设置最大值 |
setMinimum() | 设置最小值 |
setRange() | 设置取值范围(包括最大值和最小值) |
setSingleStet() | 单击上下箭头时的步长值 |
value() | 获取控件中的值 |
setStepType() | QAbstractSpinBox::DefaultStepType:固定步长,可以通过 setSingleStep() 函数设置。 QAbstractSpinBox::AdaptiveDecimalStepType:自适应步长,根据数字的大小自动调整步长。 |
setPrefix() | 设置前缀 |
setSuffix() | 设置后缀 |
生成的参考代码:
# Form implementation generated from reading ui file 'QSpinBox整数数字选择控件.ui'
#
# Created by: PyQt6 UI code generator 6.4.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.spinBox = QtWidgets.QSpinBox(parent=Form)
self.spinBox.setGeometry(QtCore.QRect(170, 90, 42, 22))
self.spinBox.setMinimum(-1)
self.spinBox.setMaximum(12)
self.spinBox.setSingleStep(2)
self.spinBox.setStepType(QtWidgets.QAbstractSpinBox.StepType.DefaultStepType)
self.spinBox.setProperty("value", 3)
self.spinBox.setObjectName("spinBox")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.spinBox.setSuffix(_translate("Form", "牛"))
self.spinBox.setPrefix(_translate("Form", "啊"))