Bonjour,

J'essaie de créer une classe de boîte de dialogue en appliquant un tuto tkinter. Comme j'apprends le python, je ne trouve pas la cause de mon erreur.
Le tuto est ici :
http://ateliers.mse.free.fr/tkinter/atelier_tkinter.pdf

Mon code est celui-ci:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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)
 
 
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.dialogBoxPacking(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):
    #je ne comprends pas pourquoi le tuto ne propose pas de constructeur :
    #def __init__(self, container, title=None, offx=250, offy=250):
        #DialogBox.__init__(self, container, title, offx, offy)
 
    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                             #Why ??
 
    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()
L'erreur est la suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"C:\Program Files (x86)\Python35-32\python.exe" C:/Users/CM/PycharmProjects/dialogbox/dialogbox.py
Traceback (most recent call last):
  File "C:/Users/CM/PycharmProjects/dialogbox/dialogbox.py", line 97, in <module>
    main = Controller()
  File "C:/Users/CM/PycharmProjects/dialogbox/dialogbox.py", line 92, in __init__
    self.view = View(self.root)
  File "C:/Users/CM/PycharmProjects/dialogbox/dialogbox.py", line 12, in __init__
    self.menuTasks.add_command(label='New Task', command=self.newTask(master))
  File "C:/Users/CM/PycharmProjects/dialogbox/dialogbox.py", line 17, in newTask
    nt = dialogBoxNewTask(master, "New Task", offx=-50, offy=-100)
  File "C:/Users/CM/PycharmProjects/dialogbox/dialogbox.py", line 74, in __init__
    DialogBox.__init__(self, container, title,offx,offy)
  File "C:/Users/CM/PycharmProjects/dialogbox/dialogbox.py", line 22, in __init__
    tk.Toplevel.__init__(self, container)
  File "C:\Program Files (x86)\Python35-32\lib\tkinter\__init__.py", line 2184, in __init__
    self.title(root.title())
TypeError: 'str' object is not callable
 
Process finished with exit code 1
Quelques explications :
DialogBox est une classe de boîte de dialogue qui propose deux boutons OK et cancel.
On remplit la boîte de dialogue en surchargeant la méthode "packing"

Je ne comprends pas l'erreur "TypeError: 'str' object is not callable" ni d'où elle provient.

quelqu'un peut-il m'aider ?

Merci