3

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()

`

zlittrell
  • 51
  • 1
  • 4

1 Answers1

11

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

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    Um okay but answering a question with a question is not what I was looking for. I figured it out myself by the way. – zlittrell Mar 06 '13 at 01:56
  • 1
    @zlittrell: I don't understand your comment. I didn't answer the question with a question. I told you specifically what the problem was (per returns None), and I also told you how I came to that conclusion. – Bryan Oakley Mar 06 '13 at 03:15
  • 1
    Saying that it returns None was not what I was asking for however, I just wanted to know how to fix the problem – zlittrell Mar 07 '13 at 01:20
  • @zlittrell: ... ? You said you have no idea what is wrong and I told you precisely what was wrong. I also told you how I figured that out. What could I have said to make this more useful? – Bryan Oakley Mar 07 '13 at 02:15
  • @BryanOakley I am having a similar problem but with `delete`, not `insert`: `self.tk.call(self._w, 'delete', first, last)`. Do you know what generally causes this? I may have to start a new thread on this but I'm afraid it may backfire. I can't post my codes: it's very long with many modules. I tried to recreate the error in fewer lines of code by my recreation works fine with no error. Confusing. – sedeh Aug 19 '14 at 19:22
  • @sedeh: I don't know how to answer you. Tkinter is rock solid in its implementation. If you're getting a similar error, you're doing something wrong. Without seeing what you're doing, it's impossible for me to even guess. You need to step through your code line-by-line, validating all of your assumptions (ie: is the index correct, is there data at that index? etc.). – Bryan Oakley Aug 19 '14 at 21:58
  • @BryanOakley Sure. I think I resolved the problem. I think I had two listboxes that weren't uniquely named and so the callback was confused. Please could you lend your voice to my tkinter question [here](http://stackoverflow.com/questions/25415888/default-to-and-select-first-item-in-tkinter-listbox). You seem to understand tkinter like no other. Thanks. – sedeh Aug 20 '14 at 23:20