【发布时间】: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)