0

I try to create a menu that show a dialog box. But the dialog box is shown before the menu. When the dialog box is closed and I click on the menu 'New Task', nothing happen.

WHY does the method newTask is called before I click on 'New Task'?

thanks for your help.

This is the code:

import tkinter as tk

class View:
    def __init__(self, master):
        master.title("Dialog Box")
        #Menu
        self.mainMenu = tk.Menu(master, tearoff=False)
        #MenuTasks
        self.menuTasks = tk.Menu(self.mainMenu, tearoff=False)
        self.menuTasks.add_command(label='New Task', command=self.newTask(master))
        self.mainMenu.add_cascade(label="Tasks", menu=self.menuTasks)
        master.config(menu=self.mainMenu)
    def newTask(self, master):
        nt = dialogBoxNewTask(master, "New Task", offx=-50, offy=-100)
        # seems to be executed when the class is instanciated
        # and no more when I click on 'New task' !?


class DialogBox(tk.Toplevel):
    def __init__(self, container, title=None, offx=250, offy=250):
        tk.Toplevel.__init__(self, container)
        self.protocol("WM_DELETE_WINDOW", self.cancel)
        self.transient(container)

        self.title("New Task")

        self.container = container
        self.result = None

        frame = tk.Frame(self)
        self.initial_focus = self.packing(frame)
        frame.pack(padx=10, pady=10)

        focusDefault = self.buttons()

        self.grab_set()

        if not self.initial_focus:
            self.initial_focus = focusDefault

        self.initial_focus.focus_set()

        self.wait_window(self)

    def packing(self, master):
        pass  # to be overloaded

    def buttons(self):
        buttonOK = tk.Button(self, text="OK", command=self.ok, default=tk.ACTIVE)
        buttonCancel = tk.Button(self, text="Cancel", command=self.cancel)
        buttonOK.pack(side=tk.LEFT, padx=5, pady=5)
        buttonCancel.pack(side=tk.LEFT, padx=5, pady=5)

        return buttonOK  # Pourquoi ??

    def ok(self, event=None):
        self.initial_focus.focus_set()
        self.withdraw()
        self.update_idletasks()
        self.apply()
        self.cancel()

    def cancel(self, event=None):
        self.container.focus_set()
        self.destroy()

    def apply(self):
        pass  # to be overloaded


 class dialogBoxNewTask(DialogBox):     
    def packing(self, master):
        tk.Label(master, text="Task :").grid (row=0, column=0)
        self.taskTask = tk.Entry(master)
        self.taskTask.grid(row=0, column=1)
        tk.Label(master, text="Due Date :").grid (row=0, column=2)
        self.taskDueDate = tk.Entry(master)
        self.taskDueDate.grid(row=0, column=3)
        return self.taskTask     
    def apply(self):
        self.result = "Result"


class Controller:
    def __init__(self):
        self.root = tk.Tk()
        #self.model = Model()
        self.view = View(self.root)

    def run(self):
        self.root.mainloop()

main = Controller()
main.run()
  • `command=self.newTask(master)` is calling the function. If you're trying to pass parameters, that isn't how you do it for tkinter – OneCricketeer Dec 05 '16 at 14:54
  • @Bryan Oakley. This question is not an exact duplicate of the one about a button. Indeed, I am new with tkinter and i can't know if my issue is coming from my use of python, tkinter or menu. So it's difficult to find an answer in so. – Pierre-Louis Deschamps Dec 06 '16 at 09:45
  • The root cause is exactly the same -- you are doing `command=foo()` rather than `command=foo`. – Bryan Oakley Dec 06 '16 at 12:19
  • 1
    I agree. I mean that it is obvious for you to find the reason of my issue which is a wrong use of the python language. But for me it seemed to be a wrong use of the tinter menu. I did not deserve a -1 vote as I could not find the solution in stack o because the previous question was about a button and not a menu. – Pierre-Louis Deschamps Dec 06 '16 at 14:27

0 Answers0