Python Tk _tkinter.TclError:无效的命令名称“.42818376”

我收到帖子标题中提到的错误我真的只是希望这个工作.一段时间以来一直在研究这个问题,这令人沮丧.我的最终目标是获得可变数据文本,chkvar和v的值.

感谢任何能够回复并提供帮助的人!

#!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

最佳答案 该程序没有通过变量获取,因此它从未报告错误的方法调用.我对原始代码做了一些更改(添加了textval StringVar,并将v变量更改为另一个IntVar).我有一种感觉“关联变量”没有问题,并且不需要包含在killwindow代码中.我在killwindow中获取的唯一变量是文本数据.

工作代码(标有#的更改行):

#!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
点赞