Jung-suk
Jung-suk

Reputation: 172

_tkinter.TclError: invalid command name ".!treeview"

I am trying to build a real time project where the status gets updated every second, so some part of code repeats continuously. when i want to change the information which has to be get updated i will just click on new button which gives me the first window where i can update the new information. but by doing so gives me the following error. Please help me with the idea to resolve this. Thank you.

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"

sample code:

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()

Upvotes: 2

Views: 1261

Answers (1)

Saad
Saad

Reputation: 3430

The issue is the module threading and mainly at line threading.Timer(1.0, self.ae).start(), threading with Tkinter is not a good idea especially with Timer of threading.

Timer can be replaced by the in-built method of Tkinter after method.

So, replace

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

with

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

Upvotes: 2

Related Questions