Python Tk _tkinter.TclError:无效的命令名称“.42818376" [英] Python Tk _tkinter.TclError: invalid command name ".42818376"

查看:28
本文介绍了Python Tk _tkinter.TclError:无效的命令名称“.42818376"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了帖子标题中提到的错误,我真的只是想让它起作用.已经在这个问题上工作了一段时间,这令人沮丧.我的最终目标是获取变量 text、chkvar 和 v 的值.

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

推荐答案

该程序没有通过变量获取,所以它从未报告不正确的方法调用.我对原始代码进行了一些更改(添加了一个 textval StringVar,并将 v 变量更改为另一个 IntVar).我有一种感觉,关联变量"不会有问题,并且不需要包含在 killwindow 代码中.我在 killwindow 中抓取的唯一变量是文本数据.

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

这篇关于Python Tk _tkinter.TclError:无效的命令名称“.42818376"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆