Tkin的平滑过渡

2024-10-01 07:46:11 发布

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

tkinter是否能够进行平滑的文本转换(缓慢出现在窗口中)?在Windows10中,python3?我试过在网上搜索,但没有类似的问题,我试着看看这个小部件是否有一个选择,但没有运气!在


Tags: 文本tkinterpython3运气windows10部件是否
2条回答

Is tkinter able to make a smooth text transition

如果你说的是tkinter.标签,则可以通过在两种颜色之间插值(开始颜色是标签的背景色,结束颜色是标签所需的前景颜色)来伪造它。下面是我想出的一个例子,其中标签从背景色(假装透明)淡入所需的前景色(本例中为红色):

import tkinter as tk

def interpolate(color_a, color_b, t):
    # 'color_a' and 'color_b' are RGB tuples
    # 't' is a value between 0.0 and 1.0
    # this is a naive interpolation
    return tuple(int(a + (b - a) * t) for a, b in zip(color_a, color_b))


class Application(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title("Font Color Test")
        self.geometry("256x64")
        self.resizable(width=False, height=False)

        self.label = tk.Label(self, text="Hello World", pady=32)
        self.label.pack()

        # On my system (Windows 7, classic theme) this is "SystemButtonFace"
        label_background_system_color = self.label.cget("background")

        label_background_16_bit_color = self.label.winfo_rgb(label_background_system_color)

        # Again, on my system, this is RGB(212, 208, 200)
        label_background_8_bit_color = tuple(value >> 8 for value in label_background_16_bit_color)

        # This isn't really required. Making a custom label foreground color just to show it doesn't have to be black.
        label_foreground_8_bit_color = tuple((255, 0, 0))

        # I want the the label to "fade in" from the background color to completely red
        self.start_color = label_background_8_bit_color
        self.end_color = label_foreground_8_bit_color

        # Let's say I want a smooth fade in transition at a rate of 60 fps and a duration of 1 second

        self.duration_ms = 1000
        self.frames_per_second = 60
        self.ms_sleep_duration = 1000 // self.frames_per_second
        self.current_step = 0

        self.update_label()


    def update_label(self):

        t = (1.0 / self.frames_per_second) * self.current_step
        self.current_step += 1

        new_color = interpolate(self.start_color, self.end_color, t)
        self.label.configure(foreground="#%02x%02x%02x" % new_color)

        if self.current_step <= self.frames_per_second:
            self.after(self.ms_sleep_duration, self.update_label)


def main():

    application = Application()
    application.mainloop()

    return 0


if __name__ == "__main__":
    import sys
    sys.exit(main())

你可以用图像来伪装。使用超时函数逐个替换它们。不确定这是否足够快,以显示顺利。在

但是对于这样的事情,我认为其他的工具箱会更适合。例如pysdl2。在

相关问题 更多 >