-
Notifications
You must be signed in to change notification settings - Fork 0
/
regular_polygons.py
277 lines (232 loc) · 8.42 KB
/
regular_polygons.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
# -------------------------
# EASTER EGG:
# import this
# -------------------------
# grid manager
from tkinter import *
from tkinter import colorchooser
import draw
# -------------------------
# GUI
menuMain = Tk()
menuMain.iconbitmap(default="images/transparent.ico")
menuMain.title("Regular Polygons")
menuMain.geometry("485x432")
menuMain['bg'] = '#f0f0f0'
# -------------------------
# VARIABLES and CONSTANTS
# SCREEN
canvas_reqheight = 362
canvas_reqwidth = 465
# SIDES
default_sides = StringVar(value="5")
value_sides = StringVar(value="5")
min_sides = 3
max_sides = 15
# RADIUS - EXPERIMENTAL
# default_radius = StringVar(value="100")
# value_radius = StringVar(value="100")
# min_radius = 1
# max_radius = 360
# ANGLE
default_angle = StringVar(value="18")
value_angle = StringVar(value="18")
min_angle = 1
max_angle = 360
# COLOR
default_color = StringVar(value="#ff0000")
value_color = StringVar(value="#ff0000")
# WIDTH
default_width = StringVar(value="100")
value_width = StringVar(value="100")
min_width = 1
max_width = 200
# -------------------------
# Class
class MenuMain:
def __init__(self):
_MenuFrame(menuMain)
_ButtonFrame(menuMain)
class _MenuFrame:
def __init__(self, master):
# -------------------------
# widgets
menu_frame = Frame(master)
# -------------------------
Label(menu_frame,
text="Sides:",
font="MicrosoftSansSerif 8"
).grid(row=0, column=0, padx=7, pady=7, sticky=NW)
Label(menu_frame,
text="Radius:",
font="MicrosoftSansSerif 8"
).grid(row=0, column=1, padx=2, pady=7, sticky=NW)
Label(menu_frame,
text="Angle:",
font="MicrosoftSansSerif 8"
).grid(row=0, column=2, padx=5, pady=7, sticky=NW)
Label(menu_frame,
text="Color:",
font="MicrosoftSansSerif 8",
).grid(row=0, column=3, padx=1, pady=7, sticky=NW)
Label(menu_frame,
text="Width:",
font="MicrosoftSansSerif 8"
).grid(row=0, column=4, padx=1, pady=7, sticky=NW)
# -------------------------
# menu_frame - row = 1
self.sb_sides = Spinbox(
menu_frame,
from_=min_sides,
to=max_sides,
textvariable=value_sides,
wrap=True,
state=NORMAL,
width=5)
# EXPERIMENTAL:
# self.sb_radius = Spinbox(
# menu_frame,
# from_=min_radius,
# to=max_radius,
# textvariable=value_radius,
# wrap=True,
# state=NORMAL,
# width=5)
self.sb_angle = Spinbox(
menu_frame,
from_=min_angle,
to=max_angle,
textvariable=value_angle,
wrap=True,
state=NORMAL,
width=5)
self.lbl_color = Label(menu_frame,
text="",
font="MicrosoftSansSerif 8",
cursor="hand2",
bg=value_color.get(),
fg="#ffffff",
width=8
)
self.sb_width = Spinbox(
menu_frame,
from_=min_width,
to=max_width,
textvariable=value_width,
wrap=True,
state=NORMAL,
width=5)
# -------------------------
# layout
self.sb_sides.grid(row=1, column=0, padx=8, pady=0, sticky=NW)
# EXPERIMENTAL:
#self.sb_radius.grid(row=1, column=1, padx=3, pady=0, sticky=NW)
self.sb_angle.grid(row=1, column=2, padx=6, pady=0, sticky=NW)
self.lbl_color.grid(row=1, column=3, padx=3, pady=0, sticky=NW)
self.lbl_color.bind('<Button-1>', self.choose_color, value_color)
self.sb_width.grid(row=1, column=4, padx=3, pady=0, sticky=NW)
menu_frame.grid(row=0, column=0)
@staticmethod
def choose_color(self):
# variable to store hexadecimal code of color
color_code = colorchooser.askcolor(title="Choose color")
value_color.set(color_code[1])
mc = _MenuFrame(menuMain)
ch_color = value_color.get()
mc.set_muda_cor_label(ch_color)
# Setter
def set_muda_cor_label(self, cor):
self.lbl_color.config(bg=cor)
class _ButtonFrame:
def __init__(self, master):
# -------------------------
# widgets
button_frame = Frame(master)
btn_draw = Button(button_frame, text="Draw TKINTER", bg="#e1e1e1", padx=10, pady=5,
command=self._button_frame_click)
btn_draw.grid(row=0, column=5, padx=98, pady=5, sticky=NW)
btn_draw.focus()
button_frame.grid(row=0, column=1)
def _button_frame_click(self):
# -----------------------------------
# Check that the data is valid
self._val_sb_sides()
# EXPERIMENTAL:
#self._val_sb_radius(value_radius.get())
self._val_sb_angle()
self._val_sb_width()
# -----------------------------------
half_reqheight = canvas_reqheight / 2
half_reqwidth = canvas_reqwidth / 2
sides = int(value_sides.get())
# EXPERIMENTAL:
#radius = int(value_radius.get())
angle = int(value_angle.get())
color = value_color.get()
width = int(value_width.get())
menu_canvas = Frame(menuMain)
dr = draw.Draw(menu_canvas, canvas_reqheight, canvas_reqwidth)
dr.draw_regular_polygon((half_reqwidth, half_reqheight), width, sides, angle, fill=color, outline=color)
menu_canvas.grid(row=2, column=0, padx=1, pady=1, columnspan=2, sticky=W)
@staticmethod
def _val_sb_sides():
# check value is integer
# check if is NOT in range
try:
if int(value_sides.get()) not in range(min_sides, max_sides + 1):
value_sides.set(default_sides.get())
except ValueError:
# Handle the exception - return to default value
value_sides.set(default_sides.get())
# # EXPERIMENTAL:
# @staticmethod
# def _val_sb_radius(val):
# mf = _MenuFrame(menuMain)
#
# # get minimum and maximum values of the widget to be validated
# min_val = int(mf.sb_radius.config('from')[4])
# max_val = int(mf.sb_radius.config('to')[4]) + 1
#
# # ---------------------------------------------------
# # print(f'\nValue Radius origin: {val}')
# # print(f'Value Radius change: {value_radius.get()}')
# # print(f'Min Radius: {min_val}')
# # print(f'Max Radius: {max_val - 1}')
# # print(f"Val: {val} NOT IN Min: {min_val}, Max: {max_val - 1}")
# # ---------------------------------------------------
#
# # check value is integer
# # check if is NOT in range
# try:
# if int(val) not in range(min_val, max_val):
# value_radius.set(default_radius.get())
# except ValueError as e:
# # Handle the exception - return to default value
# value_radius.set(default_radius.get())
# print(type(e)) # the exception type
# print(e.args) # arguments stored in .args
# e.__setstate__(None)
@staticmethod
def _val_sb_angle():
# check value is integer
# check if is NOT in range
try:
if int(value_angle.get()) not in range(min_angle, max_angle + 1):
value_angle.set(default_angle.get())
except ValueError:
# Handle the exception - return to default value
value_angle.set(default_angle.get())
@staticmethod
def _val_sb_width():
# check value is integer
# check if is NOT in range
try:
if int(value_width.get()) not in range(min_width, max_width + 1):
value_width.set(default_width.get())
except ValueError:
# Handle the exception - return to default value
value_width.set(default_width.get())
# -------------------------
# Screen
MenuMain()
menuMain.mainloop()