【问题标题】:error when exiting tkinter window update and update_idletasks退出 tkinter 窗口更新和 update_idletasks 时出错
【发布时间】:2022-11-30 23:09:03
【问题描述】:

我在 tkinter 中遇到函数 update() 和 update_idletasks() 的问题 他们工作正常,除了关闭窗口时,通过点击“退出” 按钮或“x”关闭 Windows 中的窗口,将显示以下错误行:

追溯(最近一次通话): 文件“D:\Python\VisualStudio\test4\test4\test4.py”,第 14 行,位于 label.configure(text = str(i)) # i 实际上是由一个异步函数更新的,就像一个 wifi 流 File "C:\Users\Owner\AppData\Local\Programs\Python\Python310\lib\tkinter_在里面_.py”,第 1675 行,在配置返回自我。配置('配置',cnf,kw)文件“C:\Users\Owner\AppData\Local\Programs\Python\Python310\lib\tkinter_在里面.py”,第 1665 行,在 _configure self.tk.call(_flatten((self._w, cmd)) + elf._options(cnf)) _tkinter.TclError: 无效命令名称“.!label” 按任意键继续 。 . .

最终我希望 Tkinter 显示来自 wi-fi 的传入字符,这就是我不能使用 mainloop 的原因。

【问题讨论】:

  • import tkinter as tk def increment(j): # 这模拟了我的异步函数 return j+1 root = tk.Tk() label = tk.Label(root,text="Name") label.pack() exit_button = tk. Button(root, text="Exit", command=root.destroy) exit_button.pack() i=0 while True: label.configure(text = str(i)) # i 实际上是由一个异步函数更新的,比如wifi stream i = increment(i) # 这两行只是为了模拟 root.update_idletasks() root.update()
  • 请参阅本指南了解如何提供 minimal reproducible example,并阅读有关 how to ask 的信息。请记住,如果我们不知道您已经尝试过什么,我们就无法帮助您。
  • 请不要将代码放在评论部分。您可以edit您的问题以添加所需信息。
  • 该错误告诉您您正在尝试配置已删除的小部件。

标签: python tkinter


【解决方案1】:

我想在 Tkinter 窗口上显示来自 wifi 的异步字符 经过几个问题后,我能够得出以下解决方案。 非常感谢 JRiggles 和 Bryan Oakles

这是我的源代码:

import tkinter as tk

def my_async(): # this simulates my asynchronous function, i will come from wifi
    global i
    i = i+1

i = 0
window_is_alive = True

root = tk.Tk()
label = tk.Label(root,text="Name")
label.pack()

def destroy_window():
    global window_is_alive
    window_is_alive = False

#Reassign the Close Window Control Icon
root.protocol("WM_DELETE_WINDOW", destroy_window)

exit_button = tk.Button(root, text="Exit", command=destroy_window)
exit_button.pack()

while True:
    label.configure(text = str(i)) # i is actually updated by an asynchronous function, like a wifi stream
    my_async()                     # these two lines are just to simulate that
    root.update_idletasks()
    root.update()
    if window_is_alive == False:
        root.destroy()
        break
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 2017-07-14
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    相关资源
    最近更新 更多