Skip to content Skip to sidebar Skip to footer

Tkinter Unintentional Recursion With Menu Bar Command...cause?

I'm trying to make a Python GUI using tkinter, and I need a menu item that opens another copy of the main window. I tried to do the following code, and when I ran the program, it

Solution 1:

The problem is in this line:

    file.add_command(label = 'New', command = doathing())

Here, you execute the doathing callback and then try to bind its result (which is None) to the command. In this specific case, this also leads to an infinite recursion, as the callback will create a new instance of the frame, which will again execute the callback, which will create another frame, and so on. Instead of calling the function, you have to bind the function itself to the command.

    file.add_command(label = 'New', command = doathing)  # no ()

In case you need to pass parameters to that function (not the case here) you can use a lambda:

    file.add_command(label = 'New', command = lambda: doathing(params))

Also, instead of creating another Tk instance you should probably just create a Toplevel instance in the callback, i.e.

def doathing():
    thing1 = tk.Toplevel()
    thing2 = TheThing(thing1)

Post a Comment for "Tkinter Unintentional Recursion With Menu Bar Command...cause?"