简体   繁体   English

退出时出错 tkinter window update and update_idletasks

[英]error when exiting tkinter window update and update_idletasks

I have a problem with functions update() and update_idletasks() in tkinter they work fine except that when closing the window, either by cliking the "Exit" button or the "x" to close the window in Windows, the following error lines show up:我在 tkinter 中遇到函数 update() 和 update_idletasks() 的问题,它们工作正常,除了在关闭 window 时,通过单击“退出”按钮或“x”关闭 Windows 中的 window,以下错误行显示向上:

Traceback (most recent call last): File "D:\Python\VisualStudio\test4\test4\test4.py", line 14, in label.configure(text = str(i)) # i is actually updated by an asynchronous function, like a wifi stream File "C:\Users\Owner\AppData\Local\Programs\Python\Python310\lib\tkinter_ init _.py", line 1675, in configure return self.回溯(最近一次调用最后):文件“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('configure', cnf, kw) File "C:\Users\Owner\AppData\Local\Programs\Python\Python310\lib\tkinter_ init .py", line 1665, in _configure self.tk.call(_flatten((self._w, cmd)) + elf._options(cnf)) _tkinter.TclError: invalid command name "..label" Press any key to continue.配置('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" 按任意键继续。 . . . .

Ultimately I want Tkinter to show the incoming characters from a wi-fi, which is why I cannot use mainloop.最终我希望 Tkinter 显示来自 wi-fi 的传入字符,这就是我不能使用 mainloop 的原因。

I want to display the characters coming asynchronously from a wifi on a Tkinter window After several problems I was able to come to the following solution.我想在 Tkinter window 上显示从 wifi 异步传来的字符 经过几个问题后,我得到了以下解决方案。 Many thanks to JRiggles and Bryan Oakles非常感谢 JRiggles 和 Bryan Oakles

This is my source code:这是我的源代码:

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