1

I am making a python script:

import tkinter

def print_the_nothing_thing():
    print("I told you, nothing means nothing. For further assistance, please see:")
    print("https://www.youtube.com/watch?v=3WNlWyFgLCg")
    print("Type the url into the browser.")

#setup the window
window = tkinter.Tk()
menu = tkinter.Menu(window)
window.configure(menu=menu)
view = tkinter.Menu(menu)
file = tkinter.Menu(menu)
menu.add_cascade(label="File", menu=file)
menu.add_cascade(label="View", menu=view)
#here is my problem
view.add_command(label="Nothing here!", command=print_the_nothing_thing())
file.add_command(label="Nothing here!", command=print_the_nothing_thing())
helpm.add_command(label="Help", command=helpy())
window.mainloop()

My problem is that when I define the function, it runs the function first. After closing it, the menu command does nothing.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    What evidence do you have that this is happening? And does it look like `print_the_nothing_thing` is being called twice, and then `helpy` is being called once? – Scott Hunter Jan 24 '16 at 22:23
  • What tutorial/information are you using for tkinter? I'm pretty sure that you haven't read the documentation correctly, and added parentheses where they shouldn't be. –  Jan 24 '16 at 22:26
  • @ScottHunter Yes, `print_the_nothing_thing` is 2 times and `helpy` is once. –  Jan 24 '16 at 22:30
  • Experimenting in a separate small file is an excellent practice. Keep it up. Among other things, it lets you post a near minimal file that actually runs, making it easy to find the problem. More beginners should do the same, – Terry Jan Reedy Jan 25 '16 at 07:52

2 Answers2

3

You need to remove () from those add_command lines, like

view.add_command(label="Nothing here!", command=print_the_nothing_thing)
file.add_command(label="Nothing here!", command=print_the_nothing_thing)

You are calling those functions there, and they return None. If you use just the name of the function, you get a function object.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
2

You're calling the function in add_command:

view.add_command(label="Nothing here!", command=print_the_nothing_thing())
file.add_command(label="Nothing here!", command=print_the_nothing_thing())
helpm.add_command(label="Help", command=helpy())

Instead, point add_command to the function name; don't execute it. Thus:

view.add_command(label="Nothing here!", command=print_the_nothing_thing)
file.add_command(label="Nothing here!", command=print_the_nothing_thing)
helpm.add_command(label="Help", command=helpy)
wjandrea
  • 28,235
  • 9
  • 60
  • 81