-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseconvgui.py
196 lines (157 loc) · 6.53 KB
/
baseconvgui.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/python
from string import ascii_letters, digits
import tkinter as tk
import baseconv
VALIDATION_CHOICE = 'all'
JUSTIFY_ENTRY = tk.RIGHT
def on_validate_given(to_validate, allowed_chars):
return all(char in allowed_chars for char in to_validate)
class GUI(tk.Tk):
def __init__(self, parent=None):
tk.Tk.__init__(self, parent)
self.parent = parent
self.initialise()
def initialise(self):
self.title('BaseConv')
self.resizable(width=True, height=False)
# always on top (might be windows only)
self.wm_attributes('-topmost', 1)
format_tuple = ('%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
v_num_lett = (self.register(self.on_validate_number_letter),) + format_tuple
v_num = (self.register(self.on_validate_number),) + format_tuple
v_read_only = (self.register(self.on_validate_read_only),) + format_tuple
self.v_in_num = tk.StringVar()
tk.Label(self, text='Input Number').pack(anchor=tk.W)
edt_in_num = tk.Entry(
self,
justify=JUSTIFY_ENTRY,
textvariable=self.v_in_num,
validate=VALIDATION_CHOICE,
validatecommand=v_num_lett)
edt_in_num.pack(fill=tk.X)
edt_in_num.focus_set()
self.v_in_base = tk.IntVar()
self.v_in_base.set(baseconv.DEFAULT_BASE)
tk.Label(self, text='Input Base').pack(anchor=tk.W)
tk.Entry(
self,
justify=JUSTIFY_ENTRY,
textvariable=self.v_in_base,
validate=VALIDATION_CHOICE,
validatecommand=v_num).pack(fill=tk.X)
self.v_out_base = tk.IntVar()
self.v_out_base.set(baseconv.DEFAULT_BASE)
tk.Label(self, text='Output Base').pack(anchor=tk.W)
tk.Entry(
self,
justify=JUSTIFY_ENTRY,
textvariable=self.v_out_base,
validate=VALIDATION_CHOICE,
validatecommand=v_num).pack(fill=tk.X)
f_cont = tk.Frame(self)
tk.Button(
f_cont,
text='Calculate',
command=self.calculate,
underline=1).pack(side=tk.LEFT, fill=tk.BOTH, expand=tk.YES)
tk.Button(
f_cont,
text='Swap Bases',
command=self.swap,
underline=0).pack(side=tk.LEFT, fill=tk.BOTH, expand=tk.YES)
tk.Button(
f_cont,
text='Copy',
command=self.copy,
underline=0).pack(side=tk.LEFT, fill=tk.BOTH, expand=tk.YES)
f_cont.pack(fill=tk.X)
self.v_out_num = tk.StringVar()
tk.Label(self, text='Output Number').pack(anchor=tk.W)
self.edt_out_num = tk.Entry(
self,
justify=JUSTIFY_ENTRY,
textvariable=self.v_out_num,
validate=VALIDATION_CHOICE,
validatecommand=v_read_only)
self.edt_out_num.pack(fill=tk.X)
self.bind('<Return>', self.calculate)
self.bind('<Shift-KeyRelease-Escape>', self.cancel_escape_event)
self.bind('<KeyRelease-Escape>', self.escape_event)
self.bind('<Alt-a>', self.calculate)
self.bind('<Alt-s>', self.swap)
self.bind('<Alt-c>', self.copy)
# in case the caps lock is on (yes silly it works like this)
self.bind('<Alt-A>', self.calculate)
self.bind('<Alt-S>', self.swap)
self.bind('<Alt-C>', self.copy)
# draw all the controls like "Application.ProcessMessages" in delphi
self.update_idletasks()
# then set the newly generated window as the minimum size of the window
self.minsize(self.winfo_reqwidth(), self.winfo_reqheight())
def cancel_escape_event(self, event=None):
pass
def escape_event(self, event=None):
self.destroy()
def on_validate_number_letter(self, d, i, p_upper, s, s_upper, v, v_upper, w_upper):
'''Valid percent substitutions (from the Tk entry man page)
%d = Type of action (1=insert, 0=delete, -1 for others)
%i = index of char string to be inserted/deleted, or -1
%P = value of the entry if the edit is allowed
%s = value of entry prior to editing
%S = the text string being inserted or deleted, if any
%v = the type of validation that is currently set
%V = the type of validation that triggered the callback
(key, focusin, focusout, forced)
%W = the tk name of the widget
'''
"""
print('OnValidate:')
print('d=%r' % (d))
print('i=%r' % (i))
print('P=%r' % (p_upper))
print('s=%r' % (s))
print('S=%r' % (s_upper))
print('v=%r' % (v))
print('V=%r' % (v_upper))
print('W=%r' % (w_upper))
"""
return on_validate_given(p_upper, digits+ascii_letters)
def on_validate_number(self, d, i, p_upper, s, s_upper, v, v_upper, w_upper):
return on_validate_given(p_upper, digits)
def on_validate_read_only(self, d, i, p_upper, s, s_upper, v, v_upper, w_upper):
'''Regardless of input allow no user changes.
'''
return False
def calculate(self, event=None):
try:
result = baseconv.bas_calc(
self.v_in_num.get(),
self.v_in_base.get(),
self.v_out_base.get())
except (baseconv.BaseConvError) as e:
result = e
print(result)
else:
# uses "result" variable instead of "v_out_num" tk variable as
# otherwise this line would be temporally coupled to the setting
# of that control's value, making coding awkward.
print('%r in base %r = %r in base %r' % (self.v_in_num.get(),
self.v_in_base.get(),
result,
self.v_out_base.get()))
self.v_out_num.set(result)
# Telling it to validate again as programatically filling the box with
# text means validation gets turned off which is used to
# make it readonly.
# http://coding.derkeiler.com/Archive/Tcl/comp.lang.tcl/2003-11/0618.html
self.edt_out_num.config(validate=VALIDATION_CHOICE)
def swap(self, event=None):
v_in, v_out = self.v_in_base.get(), self.v_out_base.get()
self.v_in_base.set(v_out)
self.v_out_base.set(v_in)
def copy(self, event=None):
self.clipboard_clear()
self.clipboard_append(self.v_out_num.get())
if __name__ == "__main__":
gui = GUI()
gui.mainloop()