-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGradientFrame.py
103 lines (72 loc) · 3.35 KB
/
GradientFrame.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
__author__ = "Jean Loui Bernard Silva de Jesus"
from tkinter import Canvas
class GradientFrame(Canvas):
"""
Widget with gradient colors.
"""
__tag = "GradientFrame"
__hex_format = "#%04x%04x%04x"
top2bottom = 1
left2right = 2
def __init__(self, parent, colors = ("red", "black"), direction = left2right, **kw):
# Caso o usuário não tenha configurado uma geometria, será definido uma geometria padrão.
kw["height"] = kw.get("height", 200)
kw["width"] = kw.get("width", 200)
# Chama o método construtor do Canvas.
super().__init__(parent, **kw)
# Instancia os parâmetros.
self.__geometry = [kw["width"], kw["height"]]
self.__colors = colors
self.__direction = direction
# Desenha o degradê no Canvas.
self.__draw_gradient()
def __draw_gradient(self):
"""
Paint the Canvas with gradient colors.
"""
# Apaga o degradê do Canvas.
self.delete(self.__tag)
# Recebe o limite de largura.
limit = self.__geometry[0] if self.__direction == self.left2right else self.__geometry[1]
# Recebe os valores RGB das cores.
red1, green1, blue1 = self.winfo_rgb(self.__colors[0])
red2, green2, blue2 = self.winfo_rgb(self.__colors[1])
# Calcula os valores RGB de acréscimo de cores (Ex: while red1 != red2: red1 += r_ratio)
# dividindo o mesmo pelo limite de largura.
r_ratio = (red2 - red1) / limit
g_ratio = (green2 - green1) / limit
b_ratio = (blue2 - blue1) / limit
for pixel in range(limit):
# Calcula a cor em formato RGB.
red = int(red1 + (r_ratio * pixel))
green = int(green1 + (g_ratio * pixel))
blue = int(blue1 + (b_ratio * pixel))
# Converte a cor de RGB para Hex.
color = self.__hex_format % (red, green, blue)
# Define as posições (x1, y1, x2, y2) do objeto.
x1 = pixel if self.__direction == self.left2right else 0
y1 = 0 if self.__direction == self.left2right else pixel
x2 = pixel if self.__direction == self.left2right else self.__geometry[0]
y2 = self.__geometry[1] if self.__direction == self.left2right else pixel
# Cria uma linha no canvas com uma das cores do degradê.
self.create_line(x1, y1, x2, y2, tag = self.__tag, fill = color)
# Coloca o degradê atrás de todos os elementos do Canvas.
self.tag_lower(self.__tag)
def config(self, cnf = None, **kw):
# Configura as cores do degradê.
if "colors" in kw and len(kw["colors"]) > 1:
self.__colors = kw.pop("colors")
# Configura a direção do degradê.
if "direction" in kw and kw["direction"] in (self.left2right, self.top2bottom):
self.__direction = kw.pop("direction")
# Configura a altura do degradê.
if "height" in kw:
self.__geometry[1] = kw["height"]
# Configura a largura do degradê.
if "width" in kw:
self.__geometry[0] = kw["width"]
# Configura o Canvas e desenha o degradê.
super().config(cnf, **kw)
self.__draw_gradient()
def configure(self, cnf = None, **kw):
self.config(cnf, **kw)