python - Tkinter menu command action carried out before calling, Why? -
the following producing tkinter menu 1 label "do something". running script produces "done" output immediately, means before clicking on "do something" menu label. why that? doing wrong @staticmethod? 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()
command
argument in add_command
expects function (or callable).
aren't passing function convert.do
add_command
, pass result of calling convert.do(self.file_content)
instead of it.
pass arguments convert.do
(self.file_content
in case) can use lambda
:
command=lambda self=self: convert.do(self.file_content)
Comments
Post a Comment