-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlesson22.py
More file actions
159 lines (146 loc) · 4.3 KB
/
Copy pathlesson22.py
File metadata and controls
159 lines (146 loc) · 4.3 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import sys
from functools import partial
from PyQt5.QtGui import QFont, QColor, QPallete
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication,
QWidget,
QHBoxLayout,
QVBoxLayout,
QGridLayout,
QMainWindow,
QPushButton,
QLineEdit,
QListWidget,
QLabel,
QSizePolicy)
class MyWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('Моє перше вікно')
self.setGeometry(0, 0, 500, 200)
widget = QWidget()
firstLabel = QLabel('<h1><b><i><s>Давно не бачилися!</s></i></b></h1>')
self.editArea = QLineEdit('0')
mainLayout = QHBoxLayout()
mainSubLayout = QVBoxLayout()
mainSubLayout.addWidget(self.editArea)
buttonLayout = QGridLayout()
buttons = [
{
'name': '1',
'row': 0,
'col': 0
},
{
'name': '2',
'row': 0,
'col': 1
},
{
'name': '3',
'row': 0,
'col': 2
},
{
'name': '4',
'row': 1,
'col': 0
},
{
'name': '5',
'row': 1,
'col': 1
},
{
'name': '6',
'row': 1,
'col': 2
},
{
'name': '7',
'row': 2,
'col': 0
},
{
'name': '8',
'row': 2,
'col': 1
},
{
'name': '9',
'row': 2,
'col': 2
},
{
'name': '0',
'row': 3,
'col': 0,
'colSpan': 2
},
{
'name': '=',
'row': 3,
'col': 2,
},
{
'name': '+',
'row': 0,
'col': 4,
},
{
'name': '-',
'row': 1,
'col': 4,
},
{
'name': '*',
'row': 2,
'col': 4,
},
{
'name': '/',
'row': 3,
'col': 4,
}
]
self.buttons = {}
for buttonConfig in buttons:
name = buttonConfig.get('name', '')
btn = QPushButton(name)
font = QFont()
font.setBold(True)
btn.setFont(font)
btn.setSizePolicy(QSizePolicy.Preferred,
QSizePolicy.Expanding)
btn.setFlat(True)
self.buttons[name] = btn
buttonLayout.addWidget(btn,
buttonConfig['row'],
buttonConfig['col'],
1,
buttonConfig.get('colSpan', 1))
mainSubLayout.addLayout(buttonLayout)
mainLayout.addLayout(mainSubLayout)
self.history = QListWidget()
mainLayout.addWidget(self.history)
widget.setLayout(mainLayout)
self.setCentralWidget(widget)
for buttonName in self.buttons:
btn = self.buttons[buttonName]
if buttonName == '=':
btn.clicked.connect(self.end)
else:
btn.clicked.connect(partial(self.change_text, buttonName))
def change_text(self, text):
self.editArea.setText(self.editArea.text() + text)
def end(self):
self.history.addItem(self.editArea.text() + ' = ' + '0')
self.editArea.setText('')
def main_window():
app = QApplication(sys.argv)
window = MyWindow()
window.show()
return_code = app.exec()
sys.exit(return_code)
if __name__ == '__main__':
main_window()