-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcounter.py
112 lines (86 loc) · 3.1 KB
/
counter.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
import PyQt5.QtWidgets as qtw
import PyQt5.QtCore as qtc
import PyQt5.QtGui as qtg
# Clickable label
class QClickLabel(qtw.QLabel):
clicked = qtc.pyqtSignal()
def __init__(self, parent=None):
qtw.QLabel.__init__(self, parent)
def mouseDoubleClickEvent(self, ev):
self.clicked.emit()
# LineEdit that returns focus info
class QFocusLineEdit(qtw.QLineEdit):
focusOut = qtc.pyqtSignal()
def __init__(self, parent=None):
qtw.QLineEdit.__init__(self, parent)
def focusOutEvent(self, ev):
self.focusOut.emit()
class MainWindow(qtw.QWidget):
def __init__(self):
super().__init__()
# Setup the window
self.setWindowTitle("Counter")
self.setFixedSize(240, 120)
self.setLayout(qtw.QVBoxLayout())
# Initialize variables
self.x = 0
self.label = QClickLabel(str(self.x))
self.edit = QFocusLineEdit(str(self.x))
add = qtw.QPushButton("+", clicked=lambda: self.calculate('+'))
sub = qtw.QPushButton("-", clicked=lambda: self.calculate('-'))
reset = qtw.QPushButton("0", clicked=lambda: self.calculate('0'))
grid = qtw.QWidget()
# Configure elements
self.label.setAlignment(qtc.Qt.AlignCenter)
self.label.setFont(qtg.QFont('sans-serif', 24))
self.label.clicked.connect(self.editLabel)
self.edit.setAlignment(qtc.Qt.AlignCenter)
self.edit.setFont(qtg.QFont('sans-serif', 24))
self.edit.returnPressed.connect(self.editLabelDone)
self.edit.focusOut.connect(self.editLabelDone)
self.edit.setValidator(qtg.QIntValidator())
self.edit.hide()
grid.setLayout(qtw.QGridLayout())
# Keyboard shortcuts
add.setToolTip("Add (A)")
add.setShortcut(qtc.Qt.Key_A)
sub.setToolTip("Substract (S)")
sub.setShortcut(qtc.Qt.Key_S)
reset.setToolTip("Reset (D)")
reset.setShortcut(qtc.Qt.Key_D)
self.label.setToolTip("Edit (DoubleClick/E)")
# Pack elements
grid.layout().addWidget(add, 0, 0, 1, 2)
grid.layout().addWidget(sub, 0, 2, 1, 2)
grid.layout().addWidget(reset, 0, 4)
self.layout().addWidget(self.label, alignment=qtc.Qt.AlignCenter)
self.layout().addWidget(self.edit, alignment=qtc.Qt.AlignCenter)
self.layout().addWidget(grid, alignment=qtc.Qt.AlignBottom)
# Show window
self.show()
# Edit key shortcut
def keyPressEvent(self, event):
if event.key() == qtc.Qt.Key_E:
self.editLabel()
def update(self):
self.label.setText(str(self.x))
self.edit.setText(str(self.x))
def calculate(self, op):
self.x = {
'+': self.x + 1,
'-': self.x - 1,
'0': 0}[op]
self.update()
def editLabel(self):
self.label.hide()
self.edit.show()
self.edit.setFocus()
def editLabelDone(self):
self.x = int(self.edit.text())
self.update()
self.label.show()
self.edit.hide()
app = qtw.QApplication([])
mw = MainWindow()
app.exec_()