繁体   English   中英

退出时出错 tkinter window update and update_idletasks

[英]error when exiting tkinter window update and update_idletasks

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

回溯(最近一次调用最后):文件“D:\Python\VisualStudio\test4\test4\test4.py”,第 14 行,in label.configure(text = str(i)) # i 实际上是由异步更新的 function ,就像 wifi stream 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python310\lib\tkinter_ init _.py”,第 1675 行,在配置中返回自我。 配置('configure',cnf,kw)文件“C:\Users\Owner\AppData\Local\Programs\Python\Python310\lib\tkinter_ init .py”,第 1665 行,在 _configure self.tk.call(_flatten( (self._w, cmd)) + elf._options(cnf)) _tkinter.TclError: invalid command name "..label" 按任意键继续。 . .

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

我想在 Tkinter window 上显示从 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
    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM