【问题标题】:Tkinter label text not updating, label is visible and text value is up to dateTkinter 标签文本未更新,标签可见且文本值是最新的
【发布时间】:2019-11-23 07:55:23
【问题描述】:

当尝试通过按下按钮在 tkinter 窗口上显示文本超时时,标签不会正确更新。在测试了多个不同的场景后,标签被归类为可见,它的文本值是正确的,功能本身无需按下按钮即可工作,无论使用pack,grid还是place结果都是一样的。无论标签如何更新,它都不会显示在屏幕上,只有在按钮调用该函数时才会显示。当按下按钮时,它会运行函数,沿途打印出所有内容,因此按钮本身也在工作。代码如下:

    def label_change(self, text):
    """ Repeats f|char_show (with a pause) until <text> equals the label text.

    Parameters:
        text (str): The text displayed in the label.
    """
    if self.last_txt == text:
        pass

    def char_show():
        """ Adds the next single character of <text> to the label text.

        Curtosy of Hritik Bhat on:
        stackoverflow.com/questions/56684556/how-to-have-variable-text-in-a-label-in-tkinter
        """

        if self.label_store == "":
            self.label_index = 0

        self.label_store += text[self.label_index]
        print(self.label_index)
        self.label_index += 1

        self.display.place_forget()
        self.display.config(text=self.label_store)
        self.display.update()
        self.display.place()

        print(self.display.cget("text"))
        self.display.update_idletasks()
        self.label_change(self.txt)

【问题讨论】:

  • 你如何使用它?我没有看到任何按钮来运行它。创建最少的工作代码,以便我们可以运行它。
  • 如果您需要延迟添加 char 到标签,请使用 root.after(time_ms, function_name)

标签: python tkinter text label


【解决方案1】:

如果你想添加延迟的字符,那么你可以使用root.after(time_ms, function_name) 来延迟执行函数。这不会停止mainloop(),也不需要update() 和`update_idletask()

import tkinter as tk

def start():
    global label_index

    # add next char to label
    display['text'] += text[label_index]
    label_index += 1

    # check if there is need to run it again
    if label_index < len(text):
        # run again after 200ms
        root.after(200, start)

# ---

text = 'Hello World'
label_index = 0

root = tk.Tk()

display = tk.Label(root)
display.pack()

button = tk.Button(root, text="Start", command=start)
button.pack()

root.mainloop()

编辑:此版本在更新标签时会阻止按钮。它还会在再次启动动画时重置设置。

将 tkinter 导入为 tk

def add_char():
    global label_index
    global running

    # add next char to label
    display['text'] += text[label_index]
    label_index += 1

    # check if there is need to run it again
    if label_index < len(text):
        # run again after 200ms
        root.after(200, add_char)
    else:
        # unblock button after end
        running = False

def start():
    global label_index
    global running

    # check if animation is running
    if not running:
        # block button
        running = True

        # reset settings
        display['text'] = ''
        label_index = 0

        # run animation
        add_char()

# ---

text = 'Hello World'
label_index = 0
running = False

root = tk.Tk()

display = tk.Label(root)
display.pack()

button = tk.Button(root, text="Start", command=start)
button.pack()

root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多