forked from muziing/PyQt_practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-QAbstractSpinBox-长按累加加速、只读.py
48 lines (34 loc) · 1.24 KB
/
02-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
import sys
from PyQt5.Qt import *
class MyASB(QAbstractSpinBox):
def __init__(self, parent, num: int = 0):
super().__init__(parent)
self.current_num = 0
self.lineEdit().setText(f"{num}")
def stepEnabled(self) -> "QAbstractSpinBox.StepEnabled":
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))
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)
asb.resize(120, 30)
asb.move(150, 100)
# -------累加加速--------
asb.setAccelerated(True) # 加速
print(asb.isAccelerated())
# ------只读---------
asb.setReadOnly(True) # 不能通过键盘直接修改数字,但可以通过小箭头控制数字增减
print(asb.isReadOnly())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())