| 12
 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
 
 | import tkinter as tk
from tkinter.messagebox import showinfo
 
FILE_MENU = dict(
    label = 'Fichier',
    command_items = [ 
            ('Ouvrir', 'do_open_file'),
            ('Fermer', 'do_close_file'),
            ('Enregistrer', 'do_save_file'),
            ('Quitter', 'do_quit'),
            ]
    )
 
HELP_MENU = dict(
    label = 'Aide',
    command_items = [ 
            ('A Propos', 'do_help'),
            ]                 
    )
 
def create_menu(toplevel, actions):
 
    def create_submenu(menu, command_items):
        submenu = tk.Menu(menu)
        for text, name in command_items:
            assert hasattr(actions, name)
            submenu.add_command(label=text, command=getattr(actions, name), underline = 0)
        return submenu
 
    menu = tk.Menu(toplevel)
    for zot in (FILE_MENU, HELP_MENU):
        label, command_items = zot['label'], zot['command_items']
        submenu = create_submenu(menu, command_items)
        menu.add_cascade(label = label, underline=0, menu=submenu)
    toplevel['menu'] = menu
 
class Actions:
 
    def do_quit(self): 
        app.quit()
 
    def do_help(self):
         showinfo('A propos', 'Application 1.0 \n\r Programme Python \n Montélimar 2014'
                 ' \n Auteur : JM MARTY \n Licence : GPL') 
 
    def __init__(self, canvas):
 
        self.canvas = canvas
        canvas.create_text(10 , 10, anchor ='nw', tag='text_box')
        for name in ('do_open_file', 'do_close_file', 'do_save_file'):
            action = lambda p=name: self.canvas.itemconfigure('text_box', text = p)
            setattr(self, name, action)
 
if __name__ == '__main__':
 
    app = tk.Tk()
    app.title("Mon Application")
 
    canvas = tk.Canvas(app, bg = "light grey", height = 640, width = 640)
    canvas.pack()
    actions = Actions(canvas)
 
    create_menu(app, actions)
    app.mainloop() |