-
Notifications
You must be signed in to change notification settings - Fork 217
/
01-QAbstractSpinBox-创建、模拟子类化、获取与设置控件内容.py
66 lines (51 loc) · 1.97 KB
/
01-QAbstractSpinBox-创建、模拟子类化、获取与设置控件内容.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sys
from PyQt5.Qt import *
class MyASB(QAbstractSpinBox):
"""自定义的子类,实现限制数值范围在0~9的功能"""
def __init__(self, parent, num: int = 0):
super().__init__(parent)
self.current_num = 0
self.lineEdit().setText(f"{num}")
def stepEnabled(self) -> "QAbstractSpinBox.StepEnabled":
# 目标:限制用户通过箭头输入数值在0-9范围内
try:
self.current_num = int(self.text())
except ValueError:
self.current_num = 0
if self.current_num == 0:
return QAbstractSpinBox.StepUpEnabled
elif self.current_num == 9:
return QAbstractSpinBox.StepDownEnabled
elif self.current_num < 0 or self.current_num > 9:
return QAbstractSpinBox.StepNone
else:
return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
def stepBy(self, steps: int) -> None:
self.current_num = self.current_num + steps
self.lineEdit().setText(str(self.current_num))
# 仅限于用户通过键盘方向键或者鼠标点击上下箭头改变数值
# 此时用户还可以通过键盘数字键输入0~9之外的数值
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QAbstractSpinBox")
self.resize(500, 500)
self.move(400, 250)
self.setup_ui()
def setup_ui(self):
asb = MyASB(self)
self.asb = asb
asb.resize(120, 30)
asb.move(150, 100)
test_btn = QPushButton("测试按钮", self)
test_btn.move(300, 100)
test_btn.clicked.connect(self.btn_test)
def btn_test(self):
print(self.asb.text()) # 获取文本
self.asb.lineEdit().setText("888") # 设置文本
# 不受MyASB的0~9限制,仍可设置为888
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())