-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.py
76 lines (60 loc) · 1.9 KB
/
view.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
import tkinter as tk
from tkinter import ttk
class View(tk.Tk):
PAD = 10
MAX_BUTTONS_PER_ROW = 4
button_captions = [
'C', '+/-', '%', '/',
7, 8, 9, '*',
4, 5, 6, '-',
1, 2, 3, '+',
0, '.', '='
]
def __init__(self, controller):
"""
:param controller:
"""
super().__init__()
self.title('PyTkCalc')
self.controller = controller
self.value_var = tk.StringVar()
self._make_main_frame()
self._make_entry()
self._make_buttons()
self._center_window()
def main(self):
self.mainloop()
print('In main of view')
def _make_main_frame(self):
self.main_frm = ttk.Frame(self)
self.main_frm.pack(padx=self.PAD, pady=self.PAD)
def _make_entry(self):
ent = ttk.Entry(
self.main_frm, justify='right', textvariable=self.value_var, state='disabled'
)
ent.pack(fill="x")
def _make_buttons(self):
outer_frm = ttk.Frame(self.main_frm)
outer_frm.pack()
frm = ttk.Frame(outer_frm)
frm.pack()
buttons_in_row = 0
for caption in self.button_captions:
if buttons_in_row == self.MAX_BUTTONS_PER_ROW:
frm = ttk.Frame(outer_frm)
frm.pack()
buttons_in_row = 0
btn = ttk.Button(
frm, text=caption, command=(lambda button=caption: self.controller.on_button_click(button))
)
btn.pack(side="left")
buttons_in_row += 1
def _center_window(self):
self.update()
width = self.winfo_width()
height = self.winfo_height()
x_offset = (self.winfo_screenwidth() - width) // 2
y_offset = (self.winfo_screenheight() - height) // 2
self.geometry(
f'{width}x{height}+{x_offset}+{y_offset}'
)