设置文本颜色动画,让其他小部件等待

2024-03-28 12:15:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种为各种文本创建“自定义动画”的方法。就我而言,一切都是按顺序进行的。我正在寻找一种解决方案,当我向我的方法中添加一个小部件时,小部件将在第一个小部件完成后首先播放

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()

这里的问题是“widget2”假设相同的动画,实际上该动画应该只在“widget1”完成时开始


Tags: selfinit部件def动画widgetstartmw
1条回答
网友
1楼 · 发布于 2024-03-28 12:15:24

一种简单的方法是将项目添加到队列(列表),一旦一个动画完成,就开始另一个动画

检查下面的示例

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()

相关问题 更多 >