Skip to content Skip to sidebar Skip to footer

Python Tk _tkinter.tclerror: Invalid Command Name ".42818376"

I am getting the error mentioned in the title of the post I really just want this to work. Been working on this problem for a while now and it is frustrating. My ultimate goal is

Solution 1:

Once mainloop exits, the widgets no longer exist. When you do text.get(text), you're trying to access a deleted widget. Tkinter simply isn't designed to allow you to access widgets after the main window has been destroyed.

The quick solution is to modify killwindow to get the values before it destroys the window, and store them in a global variable which you can access after mainloop exits.

Solution 2:

The program didn't make it through the variable getting, so it never reported the incorrect method calls. I made a few changes to the original code (added a textval StringVar, and changed the v variable to another IntVar). I had a feeling the "associated variables" wouldn't have a problem, and didn't need to be included in the killwindow code. The only variable I grab in killwindow is the text data.

Working code (changed lines marked with #++) :

#!C:/Python27/python.exe

from Tkinter import *
import ImageTk, Image

root = Tk()
root.title('HADOUKEN!')

textval = StringVar() #++ added

def killwindow():
    textval.set(text.get('1.0',END)) #++ grab contents before destruction
    root.destroy()

text = Text(root, height=16, width=40)
scroll = Scrollbar(root, command=text.yview)

text.configure(yscrollcommand=scroll.set)

text.grid(sticky=E)
scroll.grid(row=0,column=1,sticky='ns')

text.focus()

chkvar = IntVar()
chkvar.set(0)
c = Checkbutton(root, text="CaseIt", variable=chkvar)
c.grid(row=1,column=0,sticky=W)

v = IntVar() #++ changed
v.set(1) #++ initial value
radio1 = Radiobutton(root, text="Src", variable=v, value=1)
radio1.grid(row=1,column=0)
radio1.focus()

radio2 = Radiobutton(root, text="Dst", variable=v, value=2)
radio2.grid(row=2,column=0)

b1 = Button(root, text="Submit", command=killwindow)
b1.grid(row=1, column=2)

img = ImageTk.PhotoImage(Image.open("Hadoken.gif"))
panel = Label(root, image = img)

panel.grid(row=0, column=2)

root.mainloop()

# windows are destroyed at this point

tk1 = textval.get() #++ changed
tk2 = chkvar.get() #++ changed
tk3 = v.get() #++ changed

print tk1
print tk2
print tk3

Post a Comment for "Python Tk _tkinter.tclerror: Invalid Command Name ".42818376""