4
votes

I have no idea what is wrong but I keep getting this

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 ...?"

here is my code:`

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
Also when I hit the enter button (which creates the error) the correct answer prints in the console but not in the entry box. - zlittrell
It should be harmless, but 0.0 is an illegal index; text widget indexes start with 1.0. - Bryan Oakley

1 Answers

11
votes

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.

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?".