我如何正确地编写customtkinter语法,以便它允许我缓慢地打印ctk标签的输出,就像有人正在打字一样?

问题描述 投票:0回答:1

我正在尝试使用customtkinter创建一个游戏,我想要的是让customtkinter.CTkLabel中的文本慢慢打印出来 大多数在线资源都是针对 tkinter 的,所以我不确定如何做到这一点。 这是我正在努力解决的代码的一部分

diaglogue_var = ("Hello User")
diaglogue_var = ("Ready To Protect It")

diaglogue = customtkinter.CTkLabel(app, width=350, height=80, text=diaglogue_var)
diaglogue.pack(padx=1, pady=1)

我尝试使用从此链接获得的 def 命令

第二条评论 但不知道如何用 customtkinter.CTkLabel 来实现它,它只是一直说命令没有关闭

python tkinter customtkinter
1个回答
0
投票

这是纯 Tkinter 中的解决方案。根据我对文档的理解,您应该能够将 Tkinter 中的类名替换为 CTkinter。

首先,一些进口:

import tkinter as tk
from collections import deque

现在是自定义

StringVar
类。这将让我们逐个字符地添加到字符串中。请注意,
step
函数是所有附加发生的地方:

class TeletypeVar(tk.StringVar):
    """StringVar that appends characters from a buffer one at a time.
    
    Parameters
    ----------
    value : string, optional
        Inital string value.
    buffer : string, optional
        Initial buffer content.
    """

    def __init__(self, *args, **kwargs):
        buffer = kwargs.pop("buffer", "")
        super().__init__(*args, **kwargs)
        self.buffer = deque(buffer)
        
    def clear(self):
        """Clear contents of the string and buffer."""
        self.set("")
        self.buffer.clear()
        
    def enqueue(self, str):
        """Add the given string to the end of the buffer."""
        self.buffer.extend(str)
        
    def step(self, _event=None):
        """Move 1 character from the buffer to the string."""
        if len(self.buffer) > 0:
            self.set(self.get() + self.buffer.popleft())

创建一些东西来以稳定的速度调用回调函数,例如

TeletypeVar.step

class Clock():
    """A clock that calls ``cb`` every ``T`` milliseconds.

    Parameters
    ----------
    T : int
        Delay between calls, in milliseconds.
    cb : function
        Callback function to be repeatedly called.
    """

    def __init__(self, T, cb):
        self.T = T
        self.cb = cb
        self.after = root.after
    
    def step(self):
        """Called every T milliseconds."""
        self.cb()
        self.after(self.T, self.step)
        
    def start(self):
        """Start running the clock."""
        self.after(self.T, self.step)

这就完成了准备工作。创建框架并启动主循环应该看起来很熟悉:

class App(tk.Frame):
    def __init__(self, master):
        super().__init__(master)
        self.pack()

        self.tt_dynamic = TeletypeVar(value="Hello User",
                                      buffer="_Ready To Protect It")

        self.clk = Clock(150, self.tt_dynamic.advance)  

        dialogue = tk.Label(self, width=35, height=8,
                            textvariable=self.tt_dynamic)
        dialogue.pack(padx=1, pady=1)

        # call self.delayed_cb1() after 2 seconds
        print("before after")
        root.after(2_000, self.delayed_cb1)
        print("after after")

    def delayed_cb1(self):
        # start the clock from `__init__`
        self.clk.start()
        # call self.delayed_cb2() after a further 5 seconds
        root.after(5_000, self.delayed_cb2)
    
    def delayed_cb2(self):
        # clear out the teletype string
        self.tt_dynamic.clear()
        # speed up the clock
        self.clk.T = 75
        # and queue up more text
        self.tt_dynamic.enqueue("_And Protect It Some More")


root = tk.Tk()
myapp = App(root)
myapp.mainloop()

免责声明:我之前没有使用过 Tkinter 或 CustomTkinter,所以这可能不是最优雅的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.