Reputation: 332
I'm looking for a way to create a "custom animation" for various texts. In my case, everything is done in sequence. I'm looking for a solution where when I add a widget to my method, the widget is played first after the first widget is finished
from tkinter import *
class MainWindow(Tk):
def __init__(self):
super().__init__()
self._animate = TAnimation()
self.textLabel = Label(self, bg="black", text="FontAnimation Text1", font=("Microsoft YuHei", 30))
self.textLabel.pack(fill="both", expand="yes")
self.textLabel2 = Label(self, bg="black", text="FontAnimation Text2", font=("Microsoft YuHei", 30))
self.textLabel2.pack(fill="both", expand="yes")
self._animate.animateTextColor(self.textLabel, 0, 40, 50)
self._animate.animateTextColor(self.textLabel2, 0, 120, 100) ## want to make other widgets "waiting" maybe one sec maybe 60 secs
class TAnimation(Frame):
def __init__(self):
super().__init__()
self.r = 0
self.g = 0
self.b = 0
def animateTextColor(self, widget, start, wait, speed):
if start < wait:
widget.after(wait, lambda: self.animateTextColor(widget, start, wait, speed))
start += 5
print(start)
elif start == wait:
if self.b < 255:
widget.configure(fg=self.rgbColor((self.r, self.g, self.b)))
widget.after(speed, lambda : self.animateTextColor(widget,start, wait, speed))
self.r += 1
self.g += 1
self.b += 1
else:
self.r = 0
self.g = 0
self.b = 0
def rgbColor(self, rgb):
return "#%02x%02x%02x" % rgb
if __name__ == '__main__':
mw = MainWindow()
x1, y1 = mw.winfo_screenwidth() / 2, mw.winfo_screenheight() / 2
x2, y2 = mw.winfo_screenwidth() / 4, mw.winfo_screenheight() / 4
mw.geometry("%dx%d+%d+%d" % (x1, y1, x2, y2))
mw.mainloop()
The problem here is that "widget2" assumes the same animation, actually this animation should only start when "widget1" is finished
Upvotes: 0
Views: 70
Reputation: 3089
A simple way would be to add items to a queue(list) and once one animation is completed start another.
check the example below
from tkinter import *
class MainWindow(Tk):
def __init__(self):
super().__init__()
self._animate = TAnimation()
self.textLabel = Label(self, bg="black", text="FontAnimation Text1", font=("Microsoft YuHei", 30))
self.textLabel.pack(fill="both", expand="yes")
self.textLabel2 = Label(self, bg="black", text="FontAnimation Text2", font=("Microsoft YuHei", 30))
self.textLabel2.pack(fill="both", expand="yes")
self._animate.addToQueue(self.textLabel, 5, 20)
self._animate.addToQueue(self.textLabel2, 10, 5) ## want to make other widgets "waiting" maybe one sec maybe 60 secs
class TAnimation(Frame):
def __init__(self):
super().__init__()
self.thresh = 255
def animate(self, item, wait, rgb: list):
if any(x>=self.thresh for x in rgb):
return
rgb = [x+1 for x in rgb]
item.config(fg=self.rgbColor(rgb))
self.after(wait, self.animate, item, wait, rgb)
def addToQueue(self, widget, wait, start_after: int):
self.after(start_after, self.animate, widget, wait, [0, 0, 0])
def rgbColor(self, rgb):
return "#{0:02x}{1:02x}{2:02x}".format(*rgb)
if __name__ == '__main__':
mw = MainWindow()
x1, y1 = mw.winfo_screenwidth() / 2, mw.winfo_screenheight() / 2
x2, y2 = mw.winfo_screenwidth() / 4, mw.winfo_screenheight() / 4
mw.geometry("%dx%d+%d+%d" % (x1, y1, x2, y2))
mw.mainloop()
Upvotes: 2