-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtkeasy.py
301 lines (218 loc) · 9.94 KB
/
tkeasy.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
298
299
300
301
import os
import tkinter as tk
import logging
from tkinter import filedialog, scrolledtext, messagebox, colorchooser
#"Version 1.1"
def make_grid(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
result.grid(row = kwargs.get('row', 0),
column = kwargs.get('column', 0),
padx = kwargs.get('padx', 0),
pady = kwargs.get('pady', 0),
sticky = kwargs.get('sticky', None))
#execution of widget parameters
for param in kwargs:
try:
result.config(param=kwargs[param])
except:
#logging.warning(param)
pass
return result
return wrapper
class TKeasy():
def __init__(self,**kwargs):
self.window = tk.Tk()
#for take information from widgets
self.memory = {}
#default frame
self.frame = tk.Frame(self.window,**kwargs)
for conf in kwargs:
self.frame.config(kwargs)
self.frame.place(x=0,y=0)
#add new frame
def frames(self,frame,x,y,**kwargs):
self.frame = frame
self.frame = tk.Frame(self.window,**kwargs)
for conf in kwargs:
self.frame.config(kwargs)
self.frame.place(x = x, y = y)
def Title(self,title):
self.window.title(title)
def bind_listen(self,event,command):
return self.window.bind(event, command)
#size of window, color of background window, border and icon
def config(self,size:str = None,
bg:str = None,
border:int = None,
icon:str = None,
transparent = 1):
self.window.geometry(size) #correct syntax "300x300+300+300"
self.window.configure(background=bg)
self.window.iconbitmap(icon)
self.window.wm_attributes('-alpha', transparent)
if border == "False":
self.window.overrideredirect(1)
def show_coords(self,event):
print(f'x = {event.x_root}, y = {event.y_root}')
def move_window(self,event):
self.window.geometry(f'+{event.x_root}+{event.y_root}')
def hotkey(self,key,command):
return self.window.bind(key,command)
#LABEL WIDGET
@make_grid
def label(self, name="label", row:int = 0, column:int = 0, padx = 0, pady = 0, sticky:str = None, **kwargs):
self.memory[name] = tk.Label(self.frame, **kwargs)
return self.memory[name]
def label_click(self):
return self.memory["label"]
def image_file(self,file):
return tk.PhotoImage(file=file)
@make_grid
def photo(self, file = None, row = 0, column = 0, padx = 0, pady = 0, sticky:str = None, **kwargs):
photo = tk.PhotoImage(file=file)
self.memory["picture"] = tk.Label(self.frame,image=photo, **kwargs)
self.memory["picture"].photo = photo
return self.memory["picture"]
def photo_click(self,name = "picture"):
return self.memory[name]
@make_grid #BUTTON WIDGET
def button(self,command:str = None, text = None, row:int = 0, column:int = 0, padx = 0, pady = 0, sticky:str = None, **kwargs):
return tk.Button(self.frame,text = text,
command = command, **kwargs)
@make_grid #ENTRY WIDGET
def entry(self, name:str = "from_entry",row = 0,column = 0, padx = 0, pady = 0, sticky:str = None, **kwargs):
self.memory[name] = tk.Entry(self.frame, **kwargs)
return self.memory[name]
@make_grid
def text_area(self, name = "text_area", #Name by default
row = 0, column = 0, padx = 0, pady = 0, sticky:str = None, **kwargs):
self.memory[name] = tk.Text(self.frame, **kwargs)
return self.memory[name]
@make_grid
def text_area_scroll(self,name = "text_area_scroll", row = 0, column = 0, padx = 0, pady = 0, sticky:str = None, **kwargs):
self.memory[name] = scrolledtext.ScrolledText(self.frame, **kwargs)
return self.memory[name]
@make_grid #CHECKBUTTON WIDGET
def checkbox(self,
name:str = "checkbox", #Name by default
text:str = "", row:int = 0, column:int = 0, padx = 0, pady = 0, sticky:str = None, **kwargs):
self.memory[name] = tk.IntVar()
self.memory[text] = tk.Checkbutton(self.frame,
text = text,
variable = self.memory[name], **kwargs)
return self.memory[text]
@make_grid #RADIOBUTTON WIDGET
def radiobox(self,text,row:int = 0,column:int = 0, padx = 0, pady = 0, sticky:str = None, **kwargs):
if "radiobox" not in self.memory:
self.memory["radiobox"] = tk.StringVar()
self.memory["radiobox"].set(None)
self.memory[text] = tk.Radiobutton(self.frame, text = text, value = text, variable = self.memory["radiobox"], **kwargs)
return self.memory[text]
def dropdown_list(self,variable,choices,row = 0, column = 0, padx = 0, pady = 0, sticky:str = None, **kwargs):
self.memory[variable] = tk.StringVar(self.frame)
self.memory[variable].set(choices[0]) # default value
self.memory["dropdown"] = tk.OptionMenu(self.frame, self.memory[variable],*choices)
for conf in kwargs:
self.memory["dropdown"].config(kwargs)
self.memory["dropdown"].grid(column=column, row=row)
@make_grid #LISTBOX WIDGET
def listbox(self,name = "Listbox", row = 0, column = 0, padx = 0, pady = 0, sticky:str = None, **kwargs):
self.memory[name] = tk.Listbox(self.frame, **kwargs)
return self.memory[name]
def listbox_insert(self,name,text):
for x in text:
self.memory[name].insert("end", x)
@make_grid #SPINBOX WIDGET
def spinbox(self,name = "spinbox", from_to:str = "0-10", row = 0,column = 0, padx = 0, pady = 0, sticky:str = None, **kwargs):
data1 = int(from_to.split("-")[0])
data2 = int(from_to.split("-")[1])
self.memory[name] = tk.Spinbox(self.frame,from_=data1, to=data2, **kwargs)
return self.memory[name]
@make_grid #SCALE WIDGET
def slider(self,name="slider", from_:int = 0, to:int = 100, row = 0, column = 0, padx = 0, pady = 0, sticky:str = None, **kwargs):
self.memory[name] = tk.Scale(self.frame, from_= from_, to = to, **kwargs)
return self.memory[name]
def colorpicker(self):
return tk.colorchooser.askcolor()[1]
def msg_box(self,title,message):
return tk.messagebox.showinfo(title=title, message=message)
#alarm icon in message box
def msg_box_warning(self,title,message):
return tk.messagebox.showwarning(title=title, message=message)
def msg_box_ask(self,name,title,message):
self.memory[name] = tk.messagebox.askyesnocancel(title=title, message=message)
return self.memory[name]
#FILE NAME WITH PATH
def select_file(self):
return tk.filedialog.askopenfilename(initialdir = os.getcwd()+"./",title = "Select file")
#ONLY FILE
def only_file_name(self):
return tk.filedialog.askopenfilename(initialdir = os.getcwd()+"./",title = "Select file").split("/")[-1]
#ASK FOLDER
def select_folder(self):
return tk.filedialog.askdirectory(initialdir = os.getcwd()+"./",title = "Select folder")
def save_file(self):
return tk.filedialog.asksaveasfilename(initialdir = os.getcwd()+"./",title = "Save file")
def clear_area(self,name):
try:
self.memory[name].delete(0, 'end')
except:
pass
try:
self.memory[name].delete('1.0', tk.END)
except:
pass
def text_area_select(self,name):
return self.memory[name].selection_get()
def get_info(self,name:str):
try:
return self.memory[name].get()
except:
return self.memory[name].get("1.0", 'end')
def destroy_frame(self,**kwargs):
self.frame.destroy()
def save_file(self):
return filedialog.asksaveasfilename(initialdir = os.getcwd()+"./",title = "Save file")
def insert_text_area(self,name,text,**kwargs):
self.memory[name].insert(1.0,text)
def insert_text(self,name,text,**kwargs):
self.memory[name].insert(0,text)
def listbox_item_selected(self,name):
return self.memory[name].get("active")
def clipboard_in(self,selected,**kwargs):
self.window.clipboard_clear()
self.window.clipboard_append(selected)
def delete_selected(self,name,**kwargs):
self.memory[name].delete("sel.first", "sel.last")
def paste_text(self,name,**kwargs):
selected = self.window.clipboard_get()
position = self.memory[name].index(tk.INSERT)
self.memory[name].insert(position, selected)
def top_menu(self, tabs,**kwargs):
menu_bar = tk.Menu(self.window)
for name_of_tab in tabs:
file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label=name_of_tab, menu=file_menu)
for name_in_menu in tabs[name_of_tab]:
if name_in_menu == "---":
file_menu.add_separator()
else:
file_menu.add_command(label=name_in_menu,
command=tabs[name_of_tab][name_in_menu])
self.window.config(menu=menu_bar)
def focus(self, focus_name):
self.memory[focus_name].focus()
def clear_area(self,name_for_clear):
try:
self.memory[name_for_clear].delete(0, 'end')
except:
pass
try:
self.memory[name_for_clear].delete('1.0', tk.END)
except:
pass
def quit(self,**kwargs):
self.window.destroy()
def loop(self):
self.window.mainloop()