4

我不知道出了什么问题,但我不断得到这个

Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“/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, 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: wrong # args: 应该是“.22186144.22187184 插入索引字符?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()

`

4

1 回答 1

11

几乎唯一的方法来得到你所说的你发布的代码所得到的错误,是insert当要插入的数据是时调用该方法Nonemessage来自per, 但per返回None是因为您没有明确返回任何其他内容。

尝试调试时首先要尝试的事情之一是检查您发送给失败函数的数据是否与您认为的一样。insert您可以通过简单地打印出传递给消息的值以非常低技术的方式做到这一点。这立即告诉我那messageNone。一旦我了解了这一点,就很容易回答“为什么没有?”这个问题。

于 2013-02-01T12:39:57.180 回答