Question

Ok so basically I wrote a not very pretty GUI that gives random simple math questions. It works just like I want it to. However the idle Shell spits out red at me every time I click enter. Despite that, like I said, it continues to function as I want it to. So I'm having trouble understanding why this specific part of my code is causing problems. Apologies for the long section of code:

from tkinter import Label, Frame, Entry, Button, LEFT, RIGHT, END
from tkinter.messagebox import showinfo
import random

class Ed(Frame):
    'Simple arithmetic education app'
    def __init__(self,parent=None):
        'constructor'
        Frame.__init__(self, parent)
        self.pack()
        self.tries = 0
        Ed.make_widgets(self)
        Ed.new_problem(self)

    def make_widgets(self):
        'defines Ed widgets'
        self.entry1 = Entry(self, width=20, bg="gray", fg ="black")
        self.entry1.grid(row=0, column=0, columnspan=4)
        self.entry1.pack(side = LEFT)
        self.entry2 = Entry(self, width=20, bg="gray", fg ="black")
        self.entry2.grid(row=2, column=2, columnspan=4)
        self.entry2.pack(side = LEFT)
        Button(self,text="ENTER", command=self.evaluate).pack(side = RIGHT)

    def new_problem(self):
        'creates new arithmetic problem'
        opList = ["+", "-"]
        a = random.randrange(10+1)
        b = random.randrange(10+1)
        op = random.choice(opList)
        if b > a:
            op = "+"
        self.entry1.insert(END, a)
        self.entry1.insert(END, op)
        self.entry1.insert(END, b)


    def evaluate(self):
        'handles button "Enter" clicks by comparing answer in entry to correct result'
        result = eval(self.entry1.get())
        if result == eval(self.entry2.get()):
            self.tries += 1
            showinfo(title="Huzzah!", message="That's correct! Way to go! You got it in {} tries.".format(self.tries))
            self.entry1.delete(0, END)
            self.entry2.delete(0, END)
            self.entry1.insert(END, self.new_problem())
        else:
            self.tries += 1
            self.entry2.delete(0, END)

This is the message I get:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python32\lib\tkinter\__init__.py", line 1399, in __call__
    return self.func(*args)
  File "C:\Python32\Python shit\csc242hw4\csc242hw4.py", line 55, in evaluate
    self.entry1.insert(END, self.new_problem())
  File "C:\Python32\lib\tkinter\__init__.py", line 2385, in insert
    self.tk.call(self._w, 'insert', index, string)
_tkinter.TclError: wrong # args: should be ".52882960.52883240 insert index text"
Was it helpful?

Solution

The error message says that Tkinter thinks it's getting the wrong number of args.

Looking further back in the traceback we see this line caused the error:

File "C:\Python32\Python shit\csc242hw4\csc242hw4.py", line 55, in evaluate
    self.entry1.insert(END, self.new_problem())

But that seems like the right number of arguments! So what's going on?

The problem is that self.new_problem() returns None. It looks like Python's wrapper around Tkinter doesn't pass on the argument when it is None.

To fix this get rid of the call to insert and change line 55 to

self.new_problem()

This works because you are already calling self.entry.insert from inside new_problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top