python - Tkinter 中的平稳过渡

标签 python python-3.x user-interface tkinter

tkinter 是否能够进行平滑的文本转换(慢慢出现在窗口中)?在 Windows 10 中,Python 3 ?我试过在网上搜索但没有类似的问题,我试过看看小部件是否有这样做的选项,但没有运气!

最佳答案

Is tkinter able to make a smooth text transition

如果您谈论的是 tkinter.Label,那么您可以通过在两种颜色之间进行插值来伪造它(开始颜色是标签的背景颜色,结束颜色是标签所需的前景颜色) .这是我想出的一个示例,其中标签从背景颜色(假透明度)淡入所需的前景色(在本例中为红色):

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

关于python - Tkinter 中的平稳过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57337718/

相关文章:

c# - 在列表项之间添加空间,或使项目更容易在 ASP.NET 下拉列表中的触摸屏上选择

python - 设置 Python KafkaProducer sasl 机制属性

python - Matplotlib - 结合文本/注释坐标系

python - 在 python 进程之间传递二进制数据

java - 在 Swing 中创建符号调色板

user-interface - 用户界面和用户体验有什么区别?

python - 通过用新方法替换模型类的实例方法来模拟它

python - 从 Excel 中提取日期并使用 python 将其附加到列表中

Python 3 : write method vs. os.write 返回的字节数

python - 将日期时间存储在 numpy 数组中