0

The following is producing a Tkinter menu with one label "do something". Running the script produces the "done" output immediately, which means before even clicking on the "do something" menu label. Why is that? Am I doing something wrong about the @staticmethod? Thanks in anticipation.

import Tkinter

class AppMenu(object):

  def __init__(self, master):
    self.master = master
    self.file_content = "initialised"
    self.menu_bar(self.file_content)

  def menu_bar(self, file_content):
    menu_bar = Tkinter.Menu(self.master)
    self.menu_bar = Tkinter.Menu(self.master)
    self.master.config(menu=self.menu_bar)
    self.task_menu = Tkinter.Menu(self.menu_bar, tearoff = False)
    self.task_menu.add_command(label = "do something", command = Convert.do(self.file_content))
    self.menu_bar.add_cascade(label = "Task", menu = self.task_menu)

class Convert(object):
  @staticmethod
  def do(text):
    print "done"


root = Tkinter.Tk()
Menu = AppMenu(root)
root.mainloop()
solarisman
  • 256
  • 1
  • 4
  • 16

1 Answers1

1

command argument in add_command expects function (or something Callable).
You aren't passing function Convert.do to add_command, you pass result of calling Convert.do(self.file_content) instead of it.
To pass some arguments to Convert.do (self.file_content in your case) you can use lambda:

command=lambda self=self: Convert.do(self.file_content)
Community
  • 1
  • 1
kalgasnik
  • 3,149
  • 21
  • 19