-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.py
222 lines (170 loc) · 5.54 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
# Code generated by TkForge <https://github.com/axorax/tkforge>
# Donate to support TkForge! <https://www.patreon.com/axorax>
import os
import sys
import webbrowser
import tkinter as tk
from tk import tk_code
from tkinter import filedialog
from tkinter import messagebox
from utils import extract_figma_id
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)
window = tk.Tk()
window.geometry("750x500")
window.configure(bg="#ffffff")
window.title("TkForge")
icon_image = tk.PhotoImage(file=load_asset("icon.png"))
window.iconphoto(True, icon_image)
canvas = tk.Canvas(
window,
bg = "#ffffff",
width = 750,
height = 500,
bd = 0,
highlightthickness = 0,
relief = "ridge"
)
canvas.place(x=0, y=0)
placeholders = [
"Your Figma token",
"Figma file URL or just ID",
"Output path or leave blank to use current directory"
]
# Layout
layout = tk.PhotoImage(file=load_asset("image_1.png"))
canvas.create_image(365, 250, image=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(
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(
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(
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()
file = file_input.get()
output = outpath_input.get()
if token == "" or token in placeholders:
clear_token_input(False)
token_input.is_placeholder(False)
token_input.insert(0, "THIS IS REQUIRED")
window.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")
window.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
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!')
elif code == True:
messagebox.showinfo('Success', 'Your code has been generated!')
# Generate button
generate_button_image = tk.PhotoImage(file=load_asset("image_3.png"))
generate_button = tk.Button(
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(
image=outpath_button_image,
relief="flat",
borderwidth=0,
highlightthickness=0,
command=select_outpath
)
outpath_button.place(x=684, y=272, width=24, height=27)
# Donate button
donate_button_image = tk.PhotoImage(file=load_asset("image_4.png"))
donate_button = tk.Button(
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)
window.resizable(False, False)
window.mainloop()