|
1
|
|
|
"""A sample GUI.""" |
|
2
|
|
|
|
|
3
|
|
|
from tkinter import * |
|
4
|
|
|
from tkinter.ttk import * # type: ignore |
|
5
|
|
|
|
|
6
|
|
|
import log |
|
7
|
|
|
|
|
8
|
|
|
from . import utils |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class Application: |
|
12
|
|
|
"""A sample class.""" |
|
13
|
|
|
|
|
14
|
|
|
def __init__(self): |
|
15
|
|
|
log.info("Starting the application...") |
|
16
|
|
|
|
|
17
|
|
|
# Configure the root window |
|
18
|
|
|
self.root = Tk() |
|
19
|
|
|
self.root.title("Feet to Meters") |
|
20
|
|
|
self.root.minsize(400, 150) |
|
21
|
|
|
|
|
22
|
|
|
# Initialize elements |
|
23
|
|
|
self.feet = StringVar() |
|
24
|
|
|
self.meters = StringVar() |
|
25
|
|
|
frame = self.init(self.root) |
|
26
|
|
|
frame.pack(fill=BOTH, expand=1) |
|
27
|
|
|
|
|
28
|
|
|
# Start the event loop |
|
29
|
|
|
self.root.mainloop() |
|
30
|
|
|
|
|
31
|
|
|
def init(self, root): |
|
32
|
|
|
padded = {'padding': 5} |
|
33
|
|
|
sticky = {'sticky': NSEW} |
|
34
|
|
|
|
|
35
|
|
|
# Configure grid |
|
36
|
|
|
frame = Frame(root, **padded) |
|
37
|
|
|
frame.rowconfigure(0, weight=1) |
|
38
|
|
|
frame.rowconfigure(1, weight=1) |
|
39
|
|
|
frame.columnconfigure(0, weight=1) |
|
40
|
|
|
|
|
41
|
|
|
def frame_inputs(root): |
|
42
|
|
|
frame = Frame(root, **padded) |
|
43
|
|
|
|
|
44
|
|
|
# Configure grid |
|
45
|
|
|
frame.rowconfigure(0, weight=1) |
|
46
|
|
|
frame.rowconfigure(1, weight=1) |
|
47
|
|
|
frame.columnconfigure(0, weight=1) |
|
48
|
|
|
frame.columnconfigure(1, weight=1) |
|
49
|
|
|
frame.columnconfigure(2, weight=1) |
|
50
|
|
|
|
|
51
|
|
|
# Place widgets |
|
52
|
|
|
entry = Entry(frame, width=7, textvariable=self.feet) |
|
53
|
|
|
entry.grid(column=1, row=0) |
|
54
|
|
|
entry.focus() |
|
55
|
|
|
label = Label(frame, text="feet") |
|
56
|
|
|
label.grid(column=2, row=0) |
|
57
|
|
|
label = Label(frame, text="is equivalent to") |
|
58
|
|
|
label.grid(column=0, row=1) |
|
59
|
|
|
label = Label(frame, text="meters") |
|
60
|
|
|
label.grid(column=2, row=1) |
|
61
|
|
|
label = Label(frame, textvariable=self.meters) |
|
62
|
|
|
label.grid(column=1, row=1) |
|
63
|
|
|
|
|
64
|
|
|
return frame |
|
65
|
|
|
|
|
66
|
|
|
def frame_commands(root): |
|
67
|
|
|
frame = Frame(root, **padded) |
|
68
|
|
|
|
|
69
|
|
|
# Configure grid |
|
70
|
|
|
frame.rowconfigure(0, weight=1) |
|
71
|
|
|
frame.columnconfigure(0, weight=1) |
|
72
|
|
|
|
|
73
|
|
|
# Place widgets |
|
74
|
|
|
button = Button(frame, text="Calculate", command=self.calculate) |
|
75
|
|
|
button.grid(column=0, row=0) |
|
76
|
|
|
self.root.bind('<Return>', self.calculate) |
|
77
|
|
|
|
|
78
|
|
|
return frame |
|
79
|
|
|
|
|
80
|
|
|
def separator(root): |
|
81
|
|
|
return Separator(root) |
|
82
|
|
|
|
|
83
|
|
|
# Place widgets |
|
84
|
|
|
frame_inputs(frame).grid(row=0, **sticky) |
|
85
|
|
|
separator(frame).grid(row=1, padx=10, pady=5, **sticky) |
|
86
|
|
|
frame_commands(frame).grid(row=2, **sticky) |
|
87
|
|
|
|
|
88
|
|
|
return frame |
|
89
|
|
|
|
|
90
|
|
|
def calculate(self, event=None): |
|
91
|
|
|
log.debug("Event: %s", event) |
|
92
|
|
|
meters = utils.feet_to_meters(self.feet.get()) |
|
93
|
|
|
if meters is not None: |
|
94
|
|
|
self.meters.set(meters) |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
def main(): |
|
98
|
|
|
log.init() |
|
99
|
|
|
Application() |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
if __name__ == '__main__': # pragma: no cover |
|
103
|
|
|
main() |
|
104
|
|
|
|