TclError: 错误 # args 错误 [英] TclError: wrong # args error
问题描述
我不知道出了什么问题,但我一直收到这个
I have no idea what is wrong but I keep getting this
Tkinter 回调中的异常回溯(最近一次调用最后一次):文件/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-tk/Tkinter.py",第 1410 行,调用返回 self.func(*args)文件/Users/Zane/Desktop/Factorial GUI.py",第 72 行,在reveal2 中self.text2.insert(0.0, 消息)文件/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-tk/Tkinter.py",第 2986 行,插入self.tk.call((self._w, 'insert', index, chars) + args)TclError: 错误 # args: 应该是.22186144.22187184 插入索引字符 ?tagList chars tagList ...?"
Exception in Tkinter callback Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-tk/Tkinter.py", line 1410, in call return self.func(*args) File "/Users/Zane/Desktop/Factorial GUI.py", line 72, in reveal2 self.text2.insert(0.0, message) File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-tk/Tkinter.py", line 2986, in insert self.tk.call((self._w, 'insert', index, chars) + args) TclError: wrong # args: should be ".22186144.22187184 insert index chars ?tagList chars tagList ...?"
这是我的代码:`
from Tkinter import*
class App(Frame):
def fac(self, n):
if n >= 0:
if n == 1 or n == 0:
return 1
else:
return n*self.fac(n-1)
else:
print('Error')
def per(self, n, r):
y = (self.fac(n)) / self.fac(n - r)
print (y)
def __init__(self, master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.instruction1 = Label(self, text = "Factorial:")
self.instruction1.grid(row = 0, column = 0, columnspan = 1, sticky = W)
self.password1 = Entry(self)
self.password1.grid(row = 0, column = 1, sticky = W)
self.submit_button1 = Button(self, text ="Enter", command = self.reveal1)
self.submit_button1.grid(row = 2, column = 0, sticky = W)
self.text1 = Text(self, width = 30, height = 1, wrap = WORD)
self.text1.grid(row = 3, column = 0, columnspan = 2, sticky = W)
self.instruction2 = Label(self, text = "Permutation:")
self.instruction2.grid(row = 4, column = 0, columnspan = 1, sticky = W)
self.password2 = Entry(self)
self.password2.grid(row = 4, column = 1, sticky = W)
self.password3 = Entry(self)
self.password3.grid(row = 6, column = 1, sticky = W)
self.submit_button2 = Button(self, text ="Enter", command = self.reveal2)
self.submit_button2.grid(row = 7, column = 0, sticky = W)
self.text2 = Text(self, width = 30, height = 1, wrap = WORD)
self.text2.grid(row = 8, column = 0, columnspan = 2, sticky = W)
def reveal1(self):
y = int(self.password1.get())
message = self.fac(y)
self.text1.delete(0.0, END)
self.text1.insert(0.0, message)
def reveal2(self):
y = int(self.password2.get())
z = int(self.password3.get())
message = self.per(y, z)
self.text2.delete(0.0, END)
self.text2.insert(0.0, message)
root = Tk()
root.title('Factorial')
root.geometry("340x300")
app = App(root)
root.mainloop()
`
推荐答案
几乎唯一的方法就是在发布的代码中获得您所说的错误,就是在 insert
方法被调用时要插入的数据是None
.message
来自 per
的结果,但 per
返回 None
因为您没有明确返回任何其他内容.
Almost the only way to get the error you say you get with the code you posted, is if the insert
method is called when the data to insert is None
. message
comes from the result of per
, but per
returns None
because you don't explicitly return anything else.
尝试调试时首先要尝试的事情之一是检查您发送给失败函数的数据是否与您认为的一样.您可以通过简单地打印出传递给 insert
消息的值,以非常低技术的方式完成此操作.这立即告诉我 message
是 None
.一旦我了解了这一点,回答为什么是 None?"这个问题就很简单了.
One of the first things to try when trying to debug is to check that the data you're sending to the failing function is what you think it is. You can do this in a very low-tech way by simply printing out the values being passed to the insert
message. This instantly told me that message
was None
. Once I learned that, it's pretty simple to answer the question "why was it None?".
这篇关于TclError: 错误 # args 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!