I know it's not possible to use transparency or HSL colour representation in tkinter. But my question is if it's possible to change continuously from any colour (except black) to white, without using any other colour, just shades and hue of the chosen colour. For example I need to make my rectangle to become white continuously from brown colour in 1 minute. I have just one int/float value on the basis of which I am able to change the colour. Any ideas?
Asked
Active
Viewed 1,351 times
-1
-
Have you tried anything yet? – Dan Oberlam May 10 '14 at 20:42
-
I don't know what to try, I've been googling around to find whether it's possible to change transparency or alpha, but found out it's not possible. As the only representation of colours in tkinter is #RRGGBB or 'word' or #RRRRGGGGBBBB (025502550255) I suppose there can be only some logical way to do that but can't think of any. – TomasJ May 10 '14 at 20:45
1 Answers
3
Yes, it's possible. What you want is no different than creating a gradient between brown and white. However, instead of drawing the whole gradient you want to display one color at a time for a few milliseconds.
The following code is adapted from this answer: https://stackoverflow.com/a/11893324/7432.
Note: For demonstration purposes I have the color change over the course of 6 seconds rather than 60 so you don't have to wait as long for the full effect.
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.rect = tk.Frame(width=200, height=200)
self.rect.pack(fill="both", expand=True, padx=20, pady=20)
# compute a list of colors that form a gradient between
# a starting and ending color, then slowly adjust the background
# of the rectangle according to the computed colors
self.colors = self._compute_colors("brown", "white", 60)
self._adjust_colors()
def _adjust_colors(self):
color = self.colors.pop(0)
self.rect.configure(background=color)
if len(self.colors) > 0:
self.after(100, self._adjust_colors)
def _compute_colors(self, start, end, limit):
(r1,g1,b1) = self.winfo_rgb(start)
(r2,g2,b2) = self.winfo_rgb(end)
r_ratio = float(r2-r1) / limit
g_ratio = float(g2-g1) / limit
b_ratio = float(b2-b1) / limit
colors = []
for i in range(limit):
nr = int(r1 + (r_ratio * i))
ng = int(g1 + (g_ratio * i))
nb = int(b1 + (b_ratio * i))
color = "#%4.4x%4.4x%4.4x" % (nr,ng,nb)
colors.append(color)
return colors
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True);
root.mainloop()

Community
- 1
- 1

Bryan Oakley
- 370,779
- 53
- 539
- 685