python - _tkinter.Tcl错误: invalid command name ".!treeview"

标签 python tkinter

我正在尝试构建一个实时项目,其中状态每秒更新一次,因此代码的某些部分会不断重复。当我想要更改必须更新的信息时,我只需单击"new"按钮,这将给我第一个窗口,我可以在其中更新新信息。但这样做会给我以下错误。请帮助我想出解决这个问题的想法。谢谢。

Exception in thread Thread-2:
    Traceback (most recent call last):
      File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\threading.py", line 926, in _bootstrap_inner
        self.run()
      File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\threading.py", line 1177, in run
        self.function(*self.args, **self.kwargs)
      File "C:/Users/Desktop/Tool/t.py", line 47, in ae
        self.treeview.insert('', 'end',image=self._img, value=(a))
      File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\tkinter\ttk.py", line 1370, in insert
        res = self.tk.call(self._w, "insert", parent, index, *opts)
    _tkinter.TclError: invalid command name ".!treeview"

示例代码:

import time
import threading
import tkinter.messagebox
from tkinter import ttk
import queue
from tkinter import *

class Demo1:
    data=[]
    def __init__(self, master):#Python scrollbar on text widget
        self.master = master
        self.t=tkinter.Text(self.master,height=20,width=50)
        self.t.grid(row=1, column=1)
        self.button = tkinter.Button(self.master,height=3,width=10, text="OK", command = self.new_window)
        self.button.grid(row=2,column=1)
    def new_window(self):
        self.inputValue=self.t.get("1.0",'end-1c')
        Demo1.data1=self.inputValue.split("\n")
        self.master.destroy() # close the current window
        self.master = tkinter.Tk() # create another Tk instance
        self.app = Demo2(self.master) # create Demo2 window
        self.master.mainloop()
class Demo2:
    t1 = []
    s1 = True
    display = []
    display1 = []
    i=0
    kas = True
    num=0
    j1=0
    def __init__(self, master):
        self.master = master
        self.button = tkinter.Button(self.master,height=2,width=11, text="new",command=self.new).place(x=0,y=0)
        self.label = tkinter.Label(self.master, text="monitor", font=("Arial",20)).grid(row=0, columnspan=3)
        cols = ('aa','bb')
        self.treeview = ttk.Treeview(self.master, columns=cols)
        for col in cols:
            self.treeview.heading(col, text=col)
            self.treeview.column(col,minwidth=0,width=170)
        self.treeview.grid(row=1, column=0)
        self._img=tkinter.PhotoImage(file="green1.gif")
        self.ae()
    def ae(self):
        a=Demo1.data1
        for i,(a) in enumerate(a):
            self.treeview.insert('', 'end',image=self._img, value=(a))
        threading.Timer(1.0, self.ae).start()
    def new(self):
        self.master.destroy() # close the current window
        self.master = tkinter.Tk() # create another Tk instance
        self.app = Demo1(self.master) # create Demo2 window
        self.master.mainloop()

def main():
    root = tkinter.Tk()
    app = Demo1(root)
    root.mainloop()


if __name__ == '__main__':
    main()

最佳答案

问题出在模块 threading 上,主要在 threading.Timer(1.0, self.ae).start() 行,使用 Tkinter 进行线程化不是一个好主意尤其是线程的Timer

Timer 可以用 Tkinter 内置方法替代 after方法。

所以,替换

threading.Timer(1.0, self.ae).start()

self.treeview.after(1000, self.ae)

关于python - _tkinter.Tcl错误: invalid command name ".!treeview",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63242560/

相关文章:

python - 如何检查 Canvas 是否为空?

python - 检测用户是否点击了 'maximized'按钮

python - 如何让 tkinter treeview 适合你的框架

python - 如何为 openCV SVM 格式化数据

c++ - 我想同时等待文件描述符和互斥锁,推荐的方法是什么?

python - 编写管理器来过滤查询集结果

python - 在 pygame 中显示的缓冲区在 tkinter 中不显示

python - 在 tkinter 中创建一个简单的应用程序来显示 map

python - 如何在pytorch的固定位置插入一个值

python - Pandas 可以将可变长度空格作为列分隔符处理吗