-
Notifications
You must be signed in to change notification settings - Fork 0
/
tclwinbase_QUIDDITversion.py
248 lines (188 loc) · 8 KB
/
tclwinbase_QUIDDITversion.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
"""My Version of using Tkinter
"""
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
STDRELIEF = tk.FLAT
class TclWinBaseUsageException(BaseException):
"""Exception für Nutzungsfehler dieses Moduls
"""
def __init__(self, arg):
self.args = arg
class TclWinBase(tk.Frame):
"""base class for tcl driven windows
inherit this for your own windows
"""
def mainloop(self):
""" Enter the main loop"""
self.root.mainloop()
def __init__(self, title):
#NoDefaultRoot()
self.root = tk.Tk()
super().__init__(self.root, padx=5, pady=5)
self.root.grid_columnconfigure(0, weight=1)
self.root.grid_rowconfigure(0, weight=1)
self.root.resizable(True, True)
self.root.rowconfigure(0, weight=1)
self.root.columnconfigure(0, weight=1)
self.make_gui(title)
self.loaded()
def setwintitle(self, title):
"""Sets the title of your main window
"""
self.root.title(title)
def loaded(self):
raise TclWinBaseUsageException("Override me! Always override loaded method")
def make_gui(self, title):
raise TclWinBaseUsageException("Override me! Always override make_gui method")
def make_menu(self, menubar, title, itemlib):
submenu = tk.Menu(menubar, tearoff=0)
for item in itemlib:
submenu.add_command(label=item, command=itemlib[item])
menubar.add_cascade(label=title, menu=submenu)
return submenu
def maketext(self, lcol=0, lrow=0, erow=0, ecol=1, cspan=1, rspan=1, caption='', width=None, **options):
"""create a multiple single line text widget with a label/caption in another column
"""
tk.Label(self, text=caption).grid(row=lrow, column=lcol, columnspan=cspan, rowspan=rspan, sticky=tk.N + tk.E)
entry = tk.Text(self, **options)
if width:
entry.config(width=width)
entry.grid(row=erow, column=ecol, sticky=tk.W)
return entry
def makeentry(self, lcol=0, lrow=0, erow=0, ecol=1, caption='', width=None, **options):
"""create a single line text entry widget with a label"""
tk.Label(self, text=caption).grid(row=lrow, column=lcol, sticky=tk.E)
entry = tk.Entry(self, relief=STDRELIEF, **options)
if width:
entry.config(width=width)
entry.grid(row=erow, column=ecol, sticky=tk.W)
return entry
def make_double_entry(self, lcol=0, lrow=0, erow=0, ecol=1, caption='', width=None, **options):
"""create a single line text for number entry widget with a label
"""
tk.Label(self, text=caption).grid(row=lrow, column=lcol, sticky=tk.E)
entry = ValidateDoubleEntry(self, relief=STDRELIEF, **options)
if width:
entry.config(width=width)
entry.grid(row=erow, column=ecol, sticky=tk.W)
return entry
def make_int_entry(self, lcol=0, lrow=0, erow=0, ecol=1, caption='', width=None, **options):
"""create a single line text for number entry widget with a label
"""
tk.Label(self, text=caption).grid(row=lrow, column=lcol, sticky=tk.E)
entry = ValidateIntegerEntry(self, 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, ecol=0, erow=0, caption='', **options):
"""create a checkbox with a label"""
cb = tk.Checkbutton(self, text=caption, **options)
cb.grid(row=erow, column=ecol, sticky=tk.W)
return cb
def makebutton(self, erow=0, ecol=0, cspan=1, rspan=1, caption='Button',
width=None, cmd=None, sticky=tk.W, **options):
"""create a button widget"""
bu = tk.Button(self,
text=caption,
width=width,
command=cmd,
**options)
bu.grid(row=erow, column=ecol, columnspan=cspan, rowspan=rspan, sticky=sticky)
return bu
def makecanvas(self, erow=0, ecol=0, rspan=1, cspan=1, sticky=tk.NSEW, **options):
"""create a canvas widget"""
ca = tk.Canvas(self, **options)
ca.grid(row=erow, column=ecol,
columnspan=cspan, rowspan=rspan, sticky=sticky)
return ca
def make_mplcanvas(self, fig, erow=0, ecol=0, rspan=1, cspan=1, sticky=tk.NSEW, **options):
canvas = FigureCanvasTkAgg(fig, master=self, **options)
mpl_canvas = canvas.get_tk_widget()
mpl_canvas.grid(row=erow, column=ecol, columnspan=cspan, rowspan=rspan, sticky=sticky)
return canvas
def makelist(self, lcol=0, lrow=0, erow=0, ecol=1, caption='', width=None,
scrollvert=True, scrollhor=False,
**options):
"""create a list widget in the current window
"""
tk.Label(self, text=caption).grid(row=lrow, column=lcol, sticky=tk.N + tk.E)
if scrollvert == True:
yScroll = tk.Scrollbar(self, orient=tk.VERTICAL)
yScroll.grid(row=erow, column=ecol+1, sticky=tk.N+tk.S)
if scrollhor == True:
xScroll = tk.Scrollbar(self, orient=tk.HORIZONTAL)
xScroll.grid(row=erow+1, column=ecol, sticky=tk.E+tk.W)
lst = tk.Listbox(self, **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 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