TclError:错误的#args错误

时间:2013-02-01 05:56:55

标签: python-2.7 tkinter

我不知道出了什么问题,但我一直在想这个

  

Tkinter回调中的异常   Traceback(最近一次调用最后一次):     在电话中输入文件“/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-tk/Tkinter.py”,第1410行       return self.func(* args)     在reveal2中输入文件“/ Users / Zane / Desktop / Factorial GUI.py”,第72行       self.text2.insert(0.0,message)     文件“/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 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()

`

1 个答案:

答案 0 :(得分:9)

如果要插入的数据为insert,则调用None方法时,几乎唯一可以获得您所说的错误的唯一方法是使用您发布的代码。 message来自per的结果,但per返回None,因为您没有明确返回任何其他内容。

尝试调试时要尝试的第一件事就是检查发送到失败函数的数据是否符合您的想法。只需打印传递给insert消息的值,即可以非常低技术的方式完成此操作。这立刻告诉我messageNone。一旦我了解到这一点,回答“为什么没有?”这个问题非常简单。