-
Notifications
You must be signed in to change notification settings - Fork 1
/
QTclWindowBasics.py
297 lines (229 loc) · 11.1 KB
/
QTclWindowBasics.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
STDRELIEF = tk.FLAT
STDBG = '#ececec'
class QTclWindowBasics:
"""basic methods for popup and main windows
"""
def makelabel(self, parent=None, lrow=0, lcol=0, cspan=1, rspan=1, caption='', sticky=tk.NE, **options):
p = self if parent == None else parent
label = tk.Label(p, text=caption)
label.grid(row=lrow, column=lcol, columnspan=cspan, rowspan=rspan, sticky=sticky)
return label
def makeimagelabel(self, parent=None, lrow=0, lcol=0, cspan=1, rspan=1, img=None, caption=None, **options):
p = self if parent == None else parent
if caption != None:
label = tk.Label(p, text=caption, image=img, compound = tk.BOTTOM)
else:
label = tk.Label(p, image=img)
label.grid(row=lrow, column=lcol, columnspan=cspan, rowspan=rspan)
return label
def maketext(self, parent=None, lcol=0, lrow=0, erow=0, ecol=1, cspan=1, rspan=1, caption='', width=None, padx=(0,0), pady=(0,0), **options):
"""create a multiple single line text widget with a label/caption in another column
"""
p = self if parent == None else parent
tk.Label(p, text=caption).grid(row=lrow, column=lcol, columnspan=cspan, rowspan=rspan, sticky=tk.N + tk.E)
entry = tk.Text(p, **options)
if width:
entry.config(width=width)
entry.grid(row=erow, column=ecol, sticky=tk.W, padx=padx, pady=pady)
return entry
def makeentry(self, parent=None, lcol=0, lrow=0, erow=0, ecol=1, caption='', width=None, **options):
"""create a single line text entry widget with a label"""
p = self if parent == None else parent
tk.Label(p, text=caption).grid(row=lrow, column=lcol, sticky=tk.E)
entry = tk.Entry(p, relief=STDRELIEF, **options)
if width:
entry.config(width=width)
entry.grid(row=erow, column=ecol, sticky=tk.W)
return entry
def make_entrypair(self, parent=None, lrow=0, lcol=0, e1row=0, e1col=1, e2row=0, e2col=2, caption='', var1=None, var2=None, padx=(2,2), pady=(2,2), ewidth=None, **options):
"""Create two single line text entry widgets with a label
"""
p = self if parent == None else parent
l = self.makelabel(p, lrow=lrow, lcol=lcol, caption=caption, sticky=tk.E, padx=(padx[0],0), pady=pady)
e1 = tk.Entry(p, relief=STDRELIEF, textvariable=var1, **options)
e2 = tk.Entry(p, relief=STDRELIEF, textvariable=var2, **options)
if ewidth:
e1.config(width=ewidth)
e2.config(width=ewidth)
e1.grid(row=e1row, column=e1col, sticky=tk.EW, padx=(2,2), pady=pady)
e2.grid(row=e2row, column=e2col, sticky=tk.EW, padx=(2,padx[1]), pady=pady)
return e1, e2
def make_double_entrypair(self, parent=None, lrow=0, lcol=0, e1row=0, e1col=1, e2row=0, e2col=2, caption='', var1=None, var2=None, padx=(2,2), pady=(2,2), ewidth=None, **options):
"""Create two single line text entry widgets with a label
"""
p = self if parent == None else parent
l = self.makelabel(p, lrow=lrow, lcol=lcol, caption=caption, sticky=tk.E, padx=(padx[0],0), pady=pady)
e1 = ValidateDoubleEntry(p, relief=STDRELIEF, textvariable=var1, **options)
e2 = ValidateDoubleEntry(p, relief=STDRELIEF, textvariable=var2, **options)
if ewidth:
e1.config(width=ewidth)
e2.config(width=ewidth)
e1.grid(row=e1row, column=e1col, sticky=tk.EW, padx=(2,2), pady=pady)
e2.grid(row=e2row, column=e2col, sticky=tk.EW, padx=(2,padx[1]), pady=pady)
return e1, e2
def make_double_entry(self, parent=None, lcol=0, lrow=0, erow=0, ecol=1, caption='', width=None, padx=(2,2), pady=(2,2), **options):
"""create a single line text for a number entry widget with a label
"""
p = self if parent == None else parent
tk.Label(p, text=caption).grid(row=lrow, column=lcol, sticky=tk.E)
entry = ValidateDoubleEntry(p, relief=STDRELIEF, **options)
if width:
entry.config(width=width)
entry.grid(row=erow, column=ecol, sticky=tk.W, padx=padx, pady=pady)
return entry
def make_int_entry(self, parent=None, lcol=0, lrow=0, erow=0, ecol=1, caption='', width=None, **options):
"""create a single line text for number entry widget with a label
"""
p = self if parent == None else parent
tk.Label(p, text=caption).grid(row=lrow, column=lcol, sticky=tk.E)
entry = ValidateIntegerEntry(p, relief=STDRELIEF, **options)
if width:
entry.config(width=width)
entry.grid(row=erow, column=ecol, sticky=tk.W)
return entry
def set_entry_text(self, entry, text):
"""Set text in text entry to a given text"""
entry.delete(0, tk.END)
entry.insert(tk.END, text)
def makecheck(self, parent=None, ecol=0, erow=0, rspan=1, cspan=1, caption='', sticky=tk.W, padx=(0,0), pady=(0,0), **options):
"""create a checkbox with a label"""
p = self if parent == None else parent
cb = tk.Checkbutton(p, text=caption, **options)
cb.grid(row=erow, column=ecol, rowspan=rspan, columnspan=cspan, sticky=sticky, padx=padx, pady=pady)
return cb
def makeradio(self, parent=None, ecol=0, erow=0, cspan=1, rspan=2, caption='RadioButton',
width=None, sticky=tk.W, padx=(0,0), pady=(0,0), variable=None, value=0):
p = self if parent == None else parent
rb = tk.Radiobutton(p, text=caption, variable=variable, value=value)
rb.grid(row=erow, column=ecol, padx=padx, pady=pady, sticky=sticky)
return rb
def makebutton(self, parent=None, erow=0, ecol=0, cspan=1, rspan=1, caption='Button',
width=None, cmd=None, sticky=tk.W, padx=(0,0), pady=(0,0),**options):
"""create a button widget"""
p = self if parent == None else parent
bu = tk.Button(p,
text=caption,
width=width,
command=cmd,
**options)
bu.grid(row=erow,
column=ecol,
columnspan=cspan,
rowspan=rspan,
sticky=sticky,
padx = padx,
pady = pady)
return bu
def makecanvas(self, parent=None, erow=0, ecol=0, rspan=1, cspan=1, sticky=tk.NSEW, **options):
"""create a canvas widget"""
p = self if parent == None else parent
ca = tk.Canvas(p, **options)
ca.grid(row=erow, column=ecol,
columnspan=cspan, rowspan=rspan, sticky=sticky)
return ca
def make_mplcanvas(self, parent=None, fig=None, erow=0, ecol=0, rspan=1, cspan=1, width=None, sticky=tk.NSEW, **options):
p = self if parent == None else parent
canvas = FigureCanvasTkAgg(fig, master=p, **options)
mpl_canvas = canvas.get_tk_widget()
mpl_canvas.grid(row=erow, column=ecol, columnspan=cspan, rowspan=rspan, sticky=sticky)
# if width:
# mpl_canvas.configure(width=width)
return canvas
def makelist(self, parent=None, lcol=0, lrow=0, erow=0, ecol=1, caption='', width=None,
scrollvert=True, scrollhor=False,
**options):
"""create a list widget in the current window
"""
p = self if parent == None else parent
tk.Label(p, text=caption).grid(row=lrow, column=lcol, sticky=tk.N + tk.E)
if scrollvert == True:
yScroll = tk.Scrollbar(p, orient=tk.VERTICAL)
yScroll.grid(row=erow, column=ecol+1, sticky=tk.N+tk.S)
if scrollhor == True:
xScroll = tk.Scrollbar(p, orient=tk.HORIZONTAL)
xScroll.grid(row=erow+1, column=ecol, sticky=tk.E+tk.W)
lst = tk.Listbox(p, **options)
lst.grid(row=erow, column=ecol)
if scrollvert == True:
lst.config(yscrollcommand=yScroll.set)
yScroll['command'] = lst.yview
if scrollhor == True:
lst.config(xscrollcommand=xScroll.set)
xScroll['command'] = lst.xview
if width:
lst.config(width=width)
return lst
def make_frame(self, parent=None, ecol=0, erow=0, cspan=2, rspan=1, sticky=tk.NSEW, relief=tk.GROOVE, padx=(0,0), pady=(0,0), **options):
p = self if parent == None else parent
fr = tk.Frame(p, relief=relief, **options)
fr.grid(row=erow, column=ecol,
columnspan=cspan, rowspan=rspan, sticky=sticky,
padx=padx, pady=pady)
return fr
def make_label_frame(self, parent=None, caption="Frame", lcol=0, lrow=0, cspan=1, rspan=1, relief=tk.GROOVE, sticky=tk.NSEW, padx=(0,0), pady=(0,0), **options):
p = self if parent == None else parent
lf = tk.LabelFrame(p, relief=relief, text=caption, **options)
lf.grid(row=lrow, column=lcol,
columnspan=cspan, rowspan=rspan, sticky=sticky,
padx=padx, pady=pady)
return lf
def getvar(self, defval):
"""gets a new tkinter variable to be used for binding to entry widgets
"""
t = type(defval)
if t == str:
answ = tk.StringVar()
answ.set(defval)
elif t == int:
answ = tk.IntVar()
answ.set(defval)
elif t == float:
answ = tk.DoubleVar()
answ.set(defval)
else:
answ = tk.StringVar()
return answ
class ValidateDoubleEntry():
"""Special entry widget for editing of double numbers
"""
def __init__(self, parent, **options):
validate_number_cmd = parent.register(self.validate_number)
self.entry = tk.Entry(parent,
validate='all',
validatecommand=(validate_number_cmd, '%d', '%i', '%S'),
**options)
def config(self, **options):
self.entry.config(**options)
def grid(self, **options):
self.entry.grid(**options)
def validate_number(self, d, i, s):
if s == '':
return True
if s.isdigit() or s=='.' or (i=='0' and s=='-'):
return True
return False
def delete(self, start, end):
self.entry.delete(start, end)
def bind(self, event, command):
self.entry.bind(event, command)
class ValidateIntegerEntry():
"""special entry type widget for editing integer values
"""
def __init__(self, parent, **options):
validate_number_cmd = parent.register(self.validate_number)
self.entry = tk.Entry(parent,
validate='all',
validatecommand=(validate_number_cmd, '%d', '%i', '%S'),
**options)
def config(self, **options):
self.entry.config(**options)
def grid(self, **options):
self.entry.grid(**options)
def validate_number(self, d, i, s):
if s == '':
return True
if s.isdigit() or (i=='0' and s=='-'):
return True
return False