[英]Tkinter window got hung if update_idletasks is used instead of update
[英]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.