python - Tkinter 标签文本未更新,标签可见并且文本值是最新的

标签 python tkinter text label

当尝试通过按下按钮在 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)

最佳答案

如果你想延迟添加字符,那么你可以使用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()

关于python - Tkinter 标签文本未更新,标签可见并且文本值是最新的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57025748/

相关文章:

python-3.x - Tkinter Treeview 背景标签不起作用

php - Jquery显示来自mysql的文本

linux - 查找包含特定单词的行的终端命令?

python - setup.py 中的入口点

python - 在 Tkinter 按钮内更新 Python 字典中的值

python - 利用 Pandas 功能/使代码更 Pythonic 来重写 excel 宏

python - 将焦点上的 tkinter 标签文本斜体化

java - 如何有效地搜索字符串

python - 从文本文件提取数据到 Pandas 时如何忽略垃圾数据?

python - 将 3d Numpy 数组转换为 2d