├── GradientFrame.py ├── LICENSE └── README.md /GradientFrame.py: -------------------------------------------------------------------------------- 1 | __author__ = "Jean Loui Bernard Silva de Jesus" 2 | 3 | from tkinter import Canvas 4 | 5 | class GradientFrame(Canvas): 6 | 7 | """ 8 | Widget with gradient colors. 9 | """ 10 | 11 | __tag = "GradientFrame" 12 | __hex_format = "#%04x%04x%04x" 13 | 14 | top2bottom = 1 15 | left2right = 2 16 | 17 | def __init__(self, parent, colors = ("red", "black"), direction = left2right, **kw): 18 | 19 | # Caso o usuário não tenha configurado uma geometria, será definido uma geometria padrão. 20 | kw["height"] = kw.get("height", 200) 21 | kw["width"] = kw.get("width", 200) 22 | 23 | # Chama o método construtor do Canvas. 24 | super().__init__(parent, **kw) 25 | 26 | # Instancia os parâmetros. 27 | self.__geometry = [kw["width"], kw["height"]] 28 | self.__colors = colors 29 | self.__direction = direction 30 | 31 | # Desenha o degradê no Canvas. 32 | self.__draw_gradient() 33 | 34 | def __draw_gradient(self): 35 | 36 | """ 37 | Paint the Canvas with gradient colors. 38 | """ 39 | 40 | # Apaga o degradê do Canvas. 41 | self.delete(self.__tag) 42 | 43 | # Recebe o limite de largura. 44 | limit = self.__geometry[0] if self.__direction == self.left2right else self.__geometry[1] 45 | 46 | # Recebe os valores RGB das cores. 47 | red1, green1, blue1 = self.winfo_rgb(self.__colors[0]) 48 | red2, green2, blue2 = self.winfo_rgb(self.__colors[1]) 49 | 50 | # Calcula os valores RGB de acréscimo de cores (Ex: while red1 != red2: red1 += r_ratio) 51 | # dividindo o mesmo pelo limite de largura. 52 | r_ratio = (red2 - red1) / limit 53 | g_ratio = (green2 - green1) / limit 54 | b_ratio = (blue2 - blue1) / limit 55 | 56 | for pixel in range(limit): 57 | 58 | # Calcula a cor em formato RGB. 59 | red = int(red1 + (r_ratio * pixel)) 60 | green = int(green1 + (g_ratio * pixel)) 61 | blue = int(blue1 + (b_ratio * pixel)) 62 | 63 | # Converte a cor de RGB para Hex. 64 | color = self.__hex_format % (red, green, blue) 65 | 66 | # Define as posições (x1, y1, x2, y2) do objeto. 67 | x1 = pixel if self.__direction == self.left2right else 0 68 | y1 = 0 if self.__direction == self.left2right else pixel 69 | x2 = pixel if self.__direction == self.left2right else self.__geometry[0] 70 | y2 = self.__geometry[1] if self.__direction == self.left2right else pixel 71 | 72 | # Cria uma linha no canvas com uma das cores do degradê. 73 | self.create_line(x1, y1, x2, y2, tag = self.__tag, fill = color) 74 | 75 | # Coloca o degradê atrás de todos os elementos do Canvas. 76 | self.tag_lower(self.__tag) 77 | 78 | def config(self, cnf = None, **kw): 79 | 80 | # Configura as cores do degradê. 81 | if "colors" in kw and len(kw["colors"]) > 1: 82 | self.__colors = kw.pop("colors") 83 | 84 | # Configura a direção do degradê. 85 | if "direction" in kw and kw["direction"] in (self.left2right, self.top2bottom): 86 | self.__direction = kw.pop("direction") 87 | 88 | # Configura a altura do degradê. 89 | if "height" in kw: 90 | self.__geometry[1] = kw["height"] 91 | 92 | # Configura a largura do degradê. 93 | if "width" in kw: 94 | self.__geometry[0] = kw["width"] 95 | 96 | # Configura o Canvas e desenha o degradê. 97 | super().config(cnf, **kw) 98 | self.__draw_gradient() 99 | 100 | def configure(self, cnf = None, **kw): 101 | self.config(cnf, **kw) 102 | 103 | 104 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Jean Loui Bernard Silva de Jesus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GradientFrame-Tkinter (Python 3.x) 2 | Create a gradient widget using only the tkinter library. 3 | 4 | # How to use it ? 5 | Create an instance of GradientFrame passing as a parameter the frame where it will be placed.
6 | In addition to the Canvas settings, you can also configure the colors and direction of the gradient. 7 | 8 | # Example: 9 | 10 | ``` 11 | from GradientFrame import GradientFrame 12 | from tkinter import Tk 13 | 14 | root = Tk() 15 | gf = GradientFrame(root, colors = ("yellow", "black"), width = 800, height = 600) 16 | gf.config(direction = gf.top2bottom) 17 | gf.pack() 18 | root.mainloop() 19 | ``` 20 | --------------------------------------------------------------------------------