【问题标题】:Smooth Transition in TkinterTkinter 中的平滑过渡
【发布时间】:2019-08-03 11:03:25
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    tkinter 是否能够进行平滑的文本转换

    如果您谈论的是 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())
    

    【讨论】:

    • 如何使用新标签Toplevel
    • TopLevel 是否在弹出窗口中?如,使整个弹出窗口淡入或淡出?在那种情况下,我不知道——可能不可能。
    【解决方案2】:

    您也许可以使用图像来伪造它。使用超时功能一个接一个地替换它们。不确定这是否足够快以显示平滑。

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

    【讨论】:

    • 我才开始考虑使用另一个 GUI 开发框架。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多