-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbackend.py
More file actions
295 lines (211 loc) · 9.64 KB
/
backend.py
File metadata and controls
295 lines (211 loc) · 9.64 KB
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
import requests
import os
from tkinter import messagebox
def generate_code(token, link, output_path):
def get_color(element):
# Returns HEX form of element RGB color (str)
el_r = element["fills"][0]["color"]['r'] * 255
el_g = element["fills"][0]["color"]['g'] * 255
el_b = element["fills"][0]["color"]['b'] * 255
hex_code = ('#%02x%02x%02x' % (round(el_r), round(el_g), round(el_b)))
return hex_code
def get_coordinates(element):
# Returns element coordinates as x (int) and y (int)
x = int(element["absoluteBoundingBox"]["x"])
y = int(element["absoluteBoundingBox"]["y"])
return x, y
def get_dimensions(element):
# Return element dimensions as width (int) and height (int)
height = int(element["absoluteBoundingBox"]["height"])
width = int(element["absoluteBoundingBox"]["width"])
return width, height
def get_text_properties(element):
# Return element font and fontSize (str)
font = element["style"]["fontPostScriptName"]
fontSize = element["style"]["fontSize"]
return font, fontSize
global fig_window, response
generated_dir = output_path + "/generated_code/"
lines = []
lines.extend(['from tkinter import *\n\n',
'def btn_clicked():',
' print("Button Clicked")\n\n\n'
'window = Tk()'])
# Getting File Data
def find_between(s, first, last):
try:
start = s.index(first) + len(first)
end = s.index(last, start)
return s[start:end]
except ValueError:
return ""
token = token.strip()
file_url = link.strip()
file_id = find_between(file_url, "file/", "/")
try:
response = requests.get(
f"https://api.figma.com/v1/files/{file_id}",
headers={"X-FIGMA-TOKEN": token})
except ValueError:
messagebox.showerror(
"Value Error",
"Invalid Input. Please check your input and try again.")
except requests.ConnectionError:
messagebox.showerror(
"No Connection",
"Tkinter Designer requires internet access to work.")
data = response.json()
# Getting Window Properties
try:
fig_window = data["document"]["children"][0]["children"][0]
try:
os.mkdir(generated_dir)
except FileExistsError:
messagebox.showinfo("File Exists",
"Existing Files will be overwritten.")
except PermissionError:
messagebox.showerror("Permission Error",
"Change directory or directory permissions.")
except KeyError:
messagebox.showerror(
"Error",
"Invalid Input. Please check your input and try again.")
except IndexError:
messagebox.showerror(
"Error",
"Invalid design file. Does your file contain a Frame?")
window_width, window_height = get_dimensions(fig_window)
try:
window_bg_hex = get_color(fig_window)
except Exception as e:
print(e)
window_bg_hex = "#FFFFFF"
# Creating Window
lines.extend([f'\nwindow.geometry("{window_width}x{window_height}")',
f'window.configure(bg = "{window_bg_hex}")',
'canvas = Canvas(',
' window,',
f' bg = "{window_bg_hex}",',
f' height = {window_height},',
f' width = {window_width},',
' bd = 0,',
' highlightthickness = 0,',
' relief = "ridge")',
'canvas.place(x = 0, y = 0)\n'])
# Getting Elements inside Window
window_elements = fig_window["children"]
btn_count = 0
text_entry_count = 0
for element in window_elements:
if element["name"] == "Rectangle":
width, height = get_dimensions(element)
x, y = get_coordinates(element)
element_color = get_color(element)
lines.extend(['\ncanvas.create_rectangle(',
f' {x}, {y}, {x}+{width}, {y}+{height},',
f' fill = "{element_color}",',
' outline = "")\n'])
elif element["name"] == "Button":
width, height = get_dimensions(element)
x, y = get_coordinates(element)
item_id = element["id"]
response = requests.get(
f"https://api.figma.com/v1/images/{file_id}?ids={item_id}",
headers={"X-FIGMA-TOKEN": f"{token}"})
image_link = requests.get(response.json()["images"][item_id])
with open(f"{generated_dir}img{btn_count}.png", "wb") as file:
file.write(image_link.content)
lines.extend([
f'img{btn_count} = PhotoImage(file = f"img{btn_count}.png")',
f'b{btn_count} = Button(',
f' image = img{btn_count},',
' borderwidth = 0,',
' highlightthickness = 0,',
' command = btn_clicked,',
' relief = "flat")\n',
f'b{btn_count}.place(',
f' x = {x}, y = {y},',
f' width = {width},',
f' height = {height})\n'])
btn_count += 1
elif element["type"] == "TEXT":
text = element["characters"]
x, y = get_coordinates(element)
width, height = get_dimensions(element)
color = get_color(element)
font, fontSize = get_text_properties(element)
x, y = x + (width / 2), y + (height / 2)
text = text.replace("\n", "\\n")
lines.extend([f'canvas.create_text(',
f' {x}, {y},',
f' text = "{text}",',
f' fill = "{color}",',
f' font = ("{font}", int({fontSize})))\n'])
elif element["name"] in ("TextBox", "TextArea"):
element_types = {
"TextArea": "Text",
"TextBox": "Entry"
}
width, height = get_dimensions(element)
x, y = get_coordinates(element)
x, y = x + (width / 2), y + (height / 2)
bg = get_color(element)
item_id = element["id"]
response = requests.get(
f"https://api.figma.com/v1/images/{file_id}?ids={item_id}",
headers={"X-FIGMA-TOKEN": f"{token}"})
image_link = requests.get(response.json()["images"][item_id])
with open(
f"{generated_dir}img_textBox{text_entry_count}.png",
"wb"
) as file:
file.write(image_link.content)
lines.extend([f'entry{text_entry_count}_img = PhotoImage('
f'file = f"img_textBox{text_entry_count}.png")',
f'entry{text_entry_count}_bg = '
'canvas.create_image(',
f' {x}, {y},',
f' image = entry{text_entry_count}_img)\n'])
try:
corner_radius = element["cornerRadius"]
except KeyError:
corner_radius = 0
if corner_radius > height / 2:
corner_radius = height / 2
reduced_width = width - (corner_radius * 2)
reduced_height = height - 2
x, y = get_coordinates(element)
x = x + corner_radius
lines.extend([f'entry{text_entry_count} = '
f'{element_types[element["name"]]}(',
' bd = 0,',
f' bg = "{bg}",',
' highlightthickness = 0)\n',
f'entry{text_entry_count}.place(',
f' x = {x}, y = {y},',
f' width = {reduced_width},',
f' height = {reduced_height})\n'])
text_entry_count += 1
elif element["name"] == "Background":
width, height = get_dimensions(element)
x, y = get_coordinates(element)
x, y = x + (width / 2), y + (height / 2)
item_id = element["id"]
response = requests.get(
f"https://api.figma.com/v1/images/{file_id}"
f"?ids={item_id}&use_absolute_bounds=true",
headers={"X-FIGMA-TOKEN": f"{token}"})
image_link = requests.get(response.json()["images"][item_id])
with open(f"{generated_dir}background.png", "wb") as file:
file.write(image_link.content)
lines.extend(['background_img = PhotoImage('
'file = f"background.png")',
'background = canvas.create_image(',
f' {x}, {y},',
f' image=background_img)\n'])
# Adding Generated Code to window.py
lines.extend(['window.resizable(False, False)', 'window.mainloop()'])
final_code = [line + "\n" for line in lines]
with open(f"{generated_dir}window.py", 'w') as py_file:
py_file.writelines(final_code)
messagebox.showinfo("Success", "Files created successfully!")