Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tk _tkinter.TclError: invalid command name ".42818376"

Tags:

python

tkinter

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 to obtain the values for the varables text, chkvar, and v.

Thanks to anyone who can reply and help on this!!

#!C:/Python27/python.exe

from Tkinter import *
import ImageTk, Image

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

def killwindow():
  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 = ""
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()


tk1 = text.get(text)
tk2 = chkvar.get(chkvar)
tk3 = v.get(v)


print tk1
print tk2
print tk3
like image 399
user3495795 Avatar asked May 06 '26 23:05

user3495795


2 Answers

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.

like image 196
Bryan Oakley Avatar answered May 08 '26 13:05

Bryan Oakley


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
like image 42
RufusVS Avatar answered May 08 '26 12:05

RufusVS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!