forked from Axorax/tkforge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
277 lines (215 loc) · 6.99 KB
/
gui.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
# Code generated by TkForge <https://github.com/axorax/tkforge>
# Donate to support TkForge! <https://www.patreon.com/axorax>
import os
import sys
import random
import threading
import webbrowser
import tkinter as tk
from tk import tk_code
from utils import extract_figma_id
from tkinter import filedialog, messagebox
def load_asset(path):
base = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
assets = os.path.join(base, "assets")
return os.path.join(assets, path)
root = tk.Tk()
root.geometry("750x500")
root.configure(bg="#ffffff")
root.title("TkForge")
icon_image = tk.PhotoImage(file=load_asset("icon.png"))
root.iconphoto(True, icon_image)
current_gui = "main"
main_gui = tk.Frame(root)
main_gui.pack(fill=tk.BOTH, expand=True)
progress_gui = tk.Frame(root)
progress_gui.pack_forget()
placeholders = [
"Your Figma token",
"Figma file URL or just ID",
"Output path or leave blank to use current directory"
]
def toggle_gui():
global current_gui
if current_gui == "main":
main_gui.pack_forget()
progress_gui.pack(fill=tk.BOTH, expand=True)
current_gui = "progress"
else:
progress_gui.pack_forget()
main_gui.pack(fill=tk.BOTH, expand=True)
current_gui = "main"
# Canvas
progress_canvas = tk.Canvas(
progress_gui,
bg = "#ffffff",
width = 750,
height = 500,
bd = 0,
highlightthickness = 0,
relief = "ridge"
)
progress_canvas.place(x=0, y=0)
main_canvas = tk.Canvas(
main_gui,
bg = "#ffffff",
width = 750,
height = 500,
bd = 0,
highlightthickness = 0,
relief = "ridge"
)
main_canvas.place(x=0, y=0)
# Layout
background = tk.PhotoImage(file=load_asset(f"backgrounds/{random.randint(1, 5)}.png"))
main_canvas.create_image(167, 250, image=background)
progress_canvas.create_image(167, 250, image=background)
progress_layout = tk.PhotoImage(file=load_asset("image_5.png"))
progress_canvas.create_image(392, 253, image=progress_layout)
main_layout = tk.PhotoImage(file=load_asset("image_1.png"))
main_canvas.create_image(383, 253, image=main_layout)
# Placeholder code
class TkForge_Entry(tk.Entry):
def __init__(self, master=None, placeholder="Enter text", placeholder_fg='grey', **kwargs):
super().__init__(master, **kwargs)
self.p, self.p_fg, self.fg = placeholder, placeholder_fg, self.cget("fg")
self.putp()
self.bind("<FocusIn>", self.toggle)
self.bind("<FocusOut>", self.toggle)
def putp(self):
self.delete(0, tk.END)
self.insert(0, self.p)
self.config(fg=self.p_fg)
self.p_a = True
def toggle(self, event):
if self.p_a:
self.delete(0, tk.END)
self.config(fg=self.fg)
self.p_a = False
elif not self.get(): self.putp()
def get(self): return '' if self.p_a else super().get()
def is_placeholder(self, b):
self.p_a = b
self.config(fg=self.p_fg if b == True else self.fg)
def get_placeholder(self): return self.p
# Figma token input
token_input = TkForge_Entry(
main_gui,
bd=0,
bg="#f5f5f5",
fg="#000",
highlightthickness=0,
placeholder=placeholders[0]
)
token_input.place(x=376, y=109, width=331, height=28)
# File URL input
file_input = TkForge_Entry(
main_gui,
bd=0,
bg="#f5f5f5",
fg="#000",
highlightthickness=0,
placeholder=placeholders[1]
)
file_input.place(x=376, y=191, width=331, height=28)
# Output path textbox
outpath_input = TkForge_Entry(
main_gui,
bd=0,
bg="#f5f5f5",
fg="#000",
highlightthickness=0,
placeholder=placeholders[2]
)
outpath_input.place(x=376, y=272, width=299, height=28)
# Generate code
def clear_token_input(t=True):
token_input.delete(0, tk.END)
if t:
token_input.is_placeholder(True)
token_input.insert(0, token_input.get_placeholder())
def clear_file_input(t=True):
file_input.delete(0, tk.END)
if t:
file_input.is_placeholder(True)
file_input.insert(0, file_input.get_placeholder())
def generate():
token = token_input.get().strip().replace(' ', '')
file = file_input.get().strip().replace(' ', '')
output = outpath_input.get()
def generate_code_threaded():
nonlocal token, file, output
if token == "" or token in placeholders:
clear_token_input(False)
token_input.is_placeholder(False)
token_input.insert(0, "THIS IS REQUIRED")
root.after(1500, clear_token_input)
return
if file == "" or file in placeholders:
clear_file_input(False)
file_input.is_placeholder(False)
file_input.insert(0, "THIS IS REQUIRED")
root.after(1500, clear_file_input)
return
if output == '':
if os.path.exists('TkForge'):
response = messagebox.askyesno("Directory Already Exists", "The folder 'TkForge' already exists. Do you want to override it?")
if not response:
return
else:
if os.path.exists(os.path.join(output, 'TkForge')):
response = messagebox.askyesno("Directory Already Exists", f"The folder 'TkForge' in the directory '{output}' already exists. Do you want to override it?")
if not response:
return
toggle_gui()
code = tk_code(extract_figma_id(file), token, output)
if code == None:
messagebox.showerror('Invalid token or file', 'The file ID, token or output path that you provided is invalid!')
toggle_gui()
elif code == True:
messagebox.showinfo('Success', 'Your code has been generated!')
toggle_gui()
thread = threading.Thread(target=generate_code_threaded)
thread.start()
# Generate button
generate_button_image = tk.PhotoImage(file=load_asset("image_3.png"))
generate_button = tk.Button(
main_gui,
image=generate_button_image,
relief="flat",
borderwidth=0,
highlightthickness=0,
command=generate
)
generate_button.place(x=372, y=328, width=339, height=38)
# Output path selection
def select_outpath():
path = filedialog.askdirectory()
if path:
outpath_input.delete(0, tk.END)
outpath_input.is_placeholder(False)
outpath_input.insert(0, path)
# Output path button
outpath_button_image = tk.PhotoImage(file=load_asset("image_2.png"))
outpath_button = tk.Button(
main_gui,
image=outpath_button_image,
relief="flat",
borderwidth=0,
highlightthickness=0,
command=select_outpath
)
outpath_button.place(x=685, y=273, width=24, height=27)
# Donate button
donate_button_image = tk.PhotoImage(file=load_asset("image_4.png"))
donate_button = tk.Button(
main_gui,
image=donate_button_image,
relief="flat",
borderwidth=0,
highlightthickness=0,
command=lambda: webbrowser.open("https://www.patreon.com/axorax")
)
donate_button.place(x=371, y=446, width=343, height=34)
root.resizable(False, False)
root.mainloop()