-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtkinter_custom_button.py
276 lines (225 loc) · 12.5 KB
/
tkinter_custom_button.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
import tkinter
import sys
class TkinterCustomButton(tkinter.Frame):
""" tkinter custom button with border, rounded corners and hover effect
Arguments: master= where to place button
bg_color= background color, None is standard,
fg_color= foreground color, blue is standard,
hover_color= foreground color, lightblue is standard,
border_color= foreground color, None is standard,
border_width= border thickness, 0 is standard,
command= callback function, None is standard,
width= width of button, 110 is standard,
height= width of button, 35 is standard,
corner_radius= corner radius, 10 is standard,
text_font= (<Name>, <Size>),
text_color= text color, white is standard,
text= text of button,
hover= hover effect, True is standard,
image= PIL.PhotoImage, standard is None"""
def __init__(self,
bg_color=None,
fg_color="#2874A6",
hover_color="#5499C7",
border_color=None,
border_width=0,
command=None,
width=120,
height=40,
corner_radius=10,
text_font=None,
text_color="white",
text="CustomButton",
hover=True,
image=None,
*args, **kwargs):
super().__init__(*args, **kwargs)
if bg_color is None:
self.bg_color = self.master.cget("bg")
else:
self.bg_color = bg_color
self.fg_color = fg_color
self.hover_color = hover_color
self.border_color = border_color
self.width = width
self.height = height
if corner_radius*2 > self.height:
self.corner_radius = self.height/2
elif corner_radius*2 > self.width:
self.corner_radius = self.width/2
else:
self.corner_radius = corner_radius
self.border_width = border_width
if self.corner_radius >= self.border_width:
self.inner_corner_radius = self.corner_radius - self.border_width
else:
self.inner_corner_radius = 0
self.text = text
self.text_color = text_color
if text_font is None:
if sys.platform == "darwin": # macOS
self.text_font = ("Avenir", 13)
elif "win" in sys.platform: # Windows
self.text_font = ("Century Gothic", 11)
else:
self.text_font = ("TkDefaultFont")
else:
self.text_font = text_font
self.image = image
self.function = command
self.hover = hover
self.configure(width=self.width, height=self.height)
if sys.platform == "darwin" and self.function is not None:
self.configure(cursor="pointinghand")
self.canvas = tkinter.Canvas(master=self,
highlightthicknes=0,
background=self.bg_color,
width=self.width,
height=self.height)
self.canvas.place(x=0, y=0)
if self.hover is True:
self.canvas.bind("<Enter>", self.on_enter)
self.canvas.bind("<Leave>", self.on_leave)
self.canvas.bind("<Button-1>", self.clicked)
self.canvas.bind("<Button-1>", self.clicked)
self.canvas_fg_parts = []
self.canvas_border_parts = []
self.text_part = None
self.text_label = None
self.image_label = None
self.draw()
def draw(self):
self.canvas.delete("all")
self.canvas_fg_parts = []
self.canvas_border_parts = []
self.canvas.configure(bg=self.bg_color)
# border button parts
if self.border_width > 0:
if self.corner_radius > 0:
self.canvas_border_parts.append(self.canvas.create_oval(0,
0,
self.corner_radius * 2,
self.corner_radius * 2))
self.canvas_border_parts.append(self.canvas.create_oval(self.width - self.corner_radius * 2,
0,
self.width,
self.corner_radius * 2))
self.canvas_border_parts.append(self.canvas.create_oval(0,
self.height - self.corner_radius * 2,
self.corner_radius * 2,
self.height))
self.canvas_border_parts.append(self.canvas.create_oval(self.width - self.corner_radius * 2,
self.height - self.corner_radius * 2,
self.width,
self.height))
self.canvas_border_parts.append(self.canvas.create_rectangle(0,
self.corner_radius,
self.width,
self.height - self.corner_radius))
self.canvas_border_parts.append(self.canvas.create_rectangle(self.corner_radius,
0,
self.width - self.corner_radius,
self.height))
# inner button parts
if self.corner_radius > 0:
self.canvas_fg_parts.append(self.canvas.create_oval(self.border_width,
self.border_width,
self.border_width + self.inner_corner_radius * 2,
self.border_width + self.inner_corner_radius * 2))
self.canvas_fg_parts.append(self.canvas.create_oval(self.width - self.border_width - self.inner_corner_radius * 2,
self.border_width,
self.width - self.border_width,
self.border_width + self.inner_corner_radius * 2))
self.canvas_fg_parts.append(self.canvas.create_oval(self.border_width,
self.height - self.border_width - self.inner_corner_radius * 2,
self.border_width + self.inner_corner_radius * 2,
self.height-self.border_width))
self.canvas_fg_parts.append(self.canvas.create_oval(self.width - self.border_width - self.inner_corner_radius * 2,
self.height - self.border_width - self.inner_corner_radius * 2,
self.width - self.border_width,
self.height - self.border_width))
self.canvas_fg_parts.append(self.canvas.create_rectangle(self.border_width + self.inner_corner_radius,
self.border_width,
self.width - self.border_width - self.inner_corner_radius,
self.height - self.border_width))
self.canvas_fg_parts.append(self.canvas.create_rectangle(self.border_width,
self.border_width + self.inner_corner_radius,
self.width - self.border_width,
self.height - self.inner_corner_radius - self.border_width))
for part in self.canvas_fg_parts:
self.canvas.itemconfig(part, fill=self.fg_color, width=0)
for part in self.canvas_border_parts:
self.canvas.itemconfig(part, fill=self.border_color, width=0)
# no image given
if self.image is None:
# create tkinter.Label with text
self.text_label = tkinter.Label(master=self,
text=self.text,
font=self.text_font,
bg=self.fg_color,
fg=self.text_color)
self.text_label.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
# bind events the the button click and hover events also to the text_label
if self.hover is True:
self.text_label.bind("<Enter>", self.on_enter)
self.text_label.bind("<Leave>", self.on_leave)
self.text_label.bind("<Button-1>", self.clicked)
self.text_label.bind("<Button-1>", self.clicked)
self.set_text(self.text)
# use the given image
else:
# create tkinter.Label with image on it
self.image_label = tkinter.Label(master=self,
image=self.image,
bg=self.fg_color)
self.image_label.place(relx=0.5,
rely=0.5,
anchor=tkinter.CENTER)
# bind events the the button click and hover events also to the image_label
if self.hover is True:
self.image_label.bind("<Enter>", self.on_enter)
self.image_label.bind("<Leave>", self.on_leave)
self.image_label.bind("<Button-1>", self.clicked)
self.image_label.bind("<Button-1>", self.clicked)
def configure_color(self, bg_color=None, fg_color=None, hover_color=None, text_color=None):
if bg_color is not None:
self.bg_color = bg_color
else:
self.bg_color = self.master.cget("bg")
if fg_color is not None:
self.fg_color = fg_color
# change background color of image_label
if self.image is not None:
self.image_label.configure(bg=self.fg_color)
if hover_color is not None:
self.hover_color = hover_color
if text_color is not None:
self.text_color = text_color
if self.text_part is not None:
self.canvas.itemconfig(self.text_part, fill=self.text_color)
self.draw()
def set_text(self, text):
if self.text_label is not None:
self.text_label.configure(text=text)
def on_enter(self, event=0):
for part in self.canvas_fg_parts:
self.canvas.itemconfig(part, fill=self.hover_color, width=0)
if self.text_label is not None:
# change background color of image_label
self.text_label.configure(bg=self.hover_color)
if self.image_label is not None:
# change background color of image_label
self.image_label.configure(bg=self.hover_color)
def on_leave(self, event=0):
for part in self.canvas_fg_parts:
self.canvas.itemconfig(part, fill=self.fg_color, width=0)
if self.text_label is not None:
# change background color of image_label
self.text_label.configure(bg=self.fg_color)
if self.image_label is not None:
# change background color of image_label
self.image_label.configure(bg=self.fg_color)
def clicked(self, event=0):
if self.function is not None:
self.function()
self.on_leave()