Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All tkinter functions run when program starts

I am having a very weird problem that I've never had before when using tkinter. Anywhere that I set a command for a widget such as a button or a menu item, the command runs when the application starts up. Basically the command doesn't wait until the widget is clicked to run. In my code, I know that I did not pack the button, this was to show that widget don't even have to be drawn onto the screen for this problem to occur. Does anybody know what could be causing it? Thanks!

from tkinter import *

class menuItems(object):
    def __init__(self):
        menubar = Menu(app)
        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label="New...", command=self.new())
        filemenu.add_command(label="Open...", command=self.open())
        filemenu.add_command(label="Save", command=self.save())
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=app.quit)
        menubar.add_cascade(label="File", menu=filemenu)
        app.config(menu=menubar)

    def new(self):
        pass

    def open(self):
        pass

    def save(self):
        print("You have saved the file")

def this_should_not_run():
    print("Yay! I didn't run!")

def this_will_run_even_though_it_should_not():
    print("You can't stop me!")

def init():
    global app, menu
    app = Tk()
    app.title("Words with Python")
    app.geometry("800x500+50+50")

    menu = menuItems()

    frame = Frame(app)
    scrollbar = Scrollbar(frame, orient=VERTICAL)
    textbox = Text(frame, yscrollcommand=scrollbar.set)
    scrollbar.config(command=textbox.yview)
    scrollbar.pack(side=RIGHT, fill=Y)
    textbox.pack(side=LEFT, fill=BOTH, expand=1)
    frame.pack(fill=BOTH, expand=1)

    button = Button(app, text="Nothing", command=this_will_run_even_though_it_should_not())

    return

init()

app.mainloop()
like image 303
Sawyer Knoblich Avatar asked Jan 31 '12 02:01

Sawyer Knoblich


2 Answers

Remove the ()s in your command definitions. Right now, you are calling the function and binding the return values to command parameter whereas you need to bind the functions itself so that later on they could be called.

So a line like this:

filemenu.add_command(label="New...", command=self.new())

should actually be this:

filemenu.add_command(label="New...", command=self.new)

(You actually do this in one place correctly: filemenu.add_command(label="Exit", command=app.quit))

like image 136
Avaris Avatar answered Nov 07 '22 15:11

Avaris


filemenu.add_command(label="Open...", command=self.open())
filemenu.add_command(label="New...", command=self.new())
filemenu.add_command(label="Open...", command=self.open())
filemenu.add_command(label="Save", command=self.save())

In these lines, you have to pass the reference to the functions. You are actually calling the functions.

filemenu.add_command(label="Open...", command=self.open)
filemenu.add_command(label="New...", command=self.new)
filemenu.add_command(label="Open...", command=self.open)
filemenu.add_command(label="Save", command=self.save)
like image 42
Abhijeet Rastogi Avatar answered Nov 07 '22 13:11

Abhijeet Rastogi