-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpykidgui.py
231 lines (194 loc) · 8.64 KB
/
pykidgui.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
"""
_______________________________________________________________________
| ###### # # # # ### ###### ##### # # ### |
| # # # # # # # # # # # # # |
| # # # # # # # # # # # # # |
| ###### # ### # # # # ### # # # |
| # # # # # # # # # # # # |
| # # # # # # # # # # # # |
| # # # # ### ###### ##### ##### ### |
|_______________________________________________________________________|
"""
version = '2.0.0'
author = 'github@RonanBasto'
import tkinter as tk
from tkinter import *
class Gui(tk.Tk):
def __init__(self, title="PyKidGUI", geometry="800x600", icon_path=None):
super().__init__()
self.title(title)
self.geometry(geometry)
if icon_path:
self.iconphoto(True, tk.PhotoImage(file=icon_path))
self.menu_bar = tk.Menu(self)
self.config(menu=self.menu_bar)
self.widgets = {}
self.canvas = Canvas(self)
self.canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
self.scrollbar = Scrollbar(self, orient=VERTICAL, command=self.canvas.yview)
self.scrollbar.pack(side=RIGHT, fill=Y)
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.bind('<Configure>', lambda e: self.canvas.configure(scrollregion=self.canvas.bbox('all')))
self.frame = Frame(self.canvas)
self.canvas.create_window((0,0), window=self.frame, anchor='nw')
self.canvas.configure(yscrollcommand=self.scrollbar.set)
def add_menu(self, menu_name, menu_items):
# Cria um novo menu
new_menu = tk.Menu(self.menu_bar, tearoff=0)
# Adiciona cada item ao novo menu
for item in menu_items:
# Verifica se o item é um separador
if item == "-":
new_menu.add_separator()
else:
# Extrai o nome e a ação do item
item_name, item_action = item
# Adiciona o item ao menu com a ação associada
new_menu.add_command(label=item_name, command=item_action)
# Adiciona o novo menu à barra de menu
self.menu_bar.add_cascade(label=menu_name, menu=new_menu)
def add_image(self, img_path, **kwargs):
img = tk.PhotoImage(file=img_path)
img_label = ImageLabel(self, image=img, **kwargs)
img_label.image = img
img_label.pack()
return img_label
def add_scrollbar(self):
scrollbar = Scrollbar(self.frame, orient=VERTICAL)
scrollbar.pack(side=RIGHT, fill=Y)
return scrollbar
def update_image_position(self):
self.place(x=self.x, y=self.y)
def open_file_dialog(self, file_types):
file = filedialog.askopenfile(mode="r", filetypes=file_types)
if file:
return file.name
def add_spinbox(self, label, from_, to, callback):
var = tk.StringVar()
spinbox = tk.Spinbox(self, from_=from_, to=to, textvariable=var, command=callback)
spinbox.pack()
label_widget = tk.Label(self, text=label)
label_widget.pack()
def add_checkbox(self, text, callback):
var = tk.BooleanVar()
checkbox = tk.Checkbutton(self, text=text, variable=var, command=lambda: callback(var.get()))
checkbox.pack()
def add_text(self, text_name, prompt=""):
text_var = tk.StringVar()
new_text = tk.Entry(self, textvariable=text_var)
new_text.pack()
self.widgets[text_name] = new_text, text_var
return text_var
def add_button(self, button_name: str, on_click: Callable[[], None], text: Optional[str] = "") -> None:
new_button = tk.Button(self, text=text, command=lambda: self.new_on_click(button_name, on_click))
new_button.pack()
self.widgets[button_name] = new_button
def handle_button_click(self, button_name, on_click):
try:
on_click()
except TypeError:
exec(on_click)
def add_listbox(self, listbox_name, items):
new_listbox = tk.Listbox(self)
for item in items:
new_listbox.insert(tk.END, item)
new_listbox.pack()
self.widgets[listbox_name] = new_listbox
def add_text_area(self, text_area_name):
text_area = tk.Text(self)
text_area.pack()
self.widgets[text_area_name] = text_area
return text_area
def add_canvas(self, canvas_name, width, height):
new_canvas = tk.Canvas(self, width=width, height=height)
new_canvas.pack()
self.widgets[canvas_name] = new_canvas
return new_canvas
def add_progress_bar(self, progress_bar_name, row=0, column=0):
new_progress_bar = tk.Progressbar(self, orient=tk.HORIZONTAL, length=100, mode='determinate')
new_progress_bar.grid(row=row, column=column)
self.widgets[progress_bar_name] = new_progress_bar
return new_progress_bar
def add_canvas_items(self, canvas_name, maps):
canvas = self.widgets[canvas_name]
for m in maps:
shape = m['shape']
coords = m['coords']
fill = m.get('fill', '')
outline = m.get('outline', '')
width = m.get('width', 1)
tags = m.get('tags', '')
if shape == 'rectangle':
canvas.create_rectangle(coords, fill=fill, outline=outline, width=width, tags=tags)
elif shape == 'oval':
canvas.create_oval(coords, fill=fill, outline=outline, width=width, tags=tags)
elif shape == 'line':
canvas.create_line(coords, fill=fill, width=width, tags=tags)
elif shape == 'polygon':
canvas.create_polygon(coords, fill=fill, outline=outline, width=width, tags=tags)
def update_canvas_items(self, canvas_name, maps):
canvas = self.widgets[canvas_name]
canvas.delete('all')
self.add_canvas_items(canvas_name, maps)
def clear_canvas(self, canvas_name):
canvas = self.widgets[canvas_name]
canvas.delete('all')
def new_on_click(self, button_name, on_click):
if isinstance(self.widgets[button_name], tuple):
_, text_var = self.widgets[button_name]
on_click(text_var.get())
elif isinstance(self.widgets[button_name], tk.Listbox):
selected_index = self.widgets[button_name].curselection()
on_click(selected_index)
else:
on_click()
def add_entry(self, entry_name):
entry = tk.Entry(self)
entry.pack()
return entry
def get_entry_value(self, entry):
return entry.get()
def add_label(self, text):
label = Label(self.frame, text=text)
label.pack()
class Canvas_Map(tk.Canvas):
def __init__(self, master, map_data, cell_size=50):
super().__init__(master, width=len(map_data[0]) * cell_size, height=len(map_data) * cell_size)
self.map_data = map_data
self.cell_size = cell_size
self.cells = []
self.draw_map()
def draw_map(self):
for y, row in enumerate(self.map_data):
for x, value in enumerate(row):
x1, y1 = x * self.cell_size, y * self.cell_size
x2, y2 = x1 + self.cell_size, y1 + self.cell_size
cell = self.create_rectangle(x1, y1, x2, y2, fill=value)
self.cells.append(cell)
class GUI_FRAME:
def __init__(self, title, geometry):
self.root = tk.Tk()
self.root.title(title)
self.root.geometry(geometry)
self.frames = {}
def add_frame(self, frame_name, **kwargs):
frame = tk.Frame(self.root, **kwargs)
self.frames[frame_name] = frame
frame.pack()
def run(self):
self.root.mainloop()
class ImageLabel(tk.Label):
def __init__(self, master=None, **kwargs):
super().__init__(master, **kwargs)
self.image = None
self.x = 0
self.y = 0
def set_image(self, img_path):
self.image = tk.PhotoImage(file=img_path)
self.config(image=self.image)
def update_position(self):
self.place(x=self.x, y=self.y)
def set_position(self, x, y):
self.x = x
self.y = y
self.update_position()