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
| #! -*- coding:utf-8 -*-
import tkinter as tk
from tkinter.constants import *
from tkinter import messagebox
showinfo = messagebox.showinfo
STATES = [ DISABLED, NORMAL ]
TclError = tk._tkinter.TclError
def do_toggle(menu, label):
try:
ix = menu.index(label)
except TclError:
return
state = menu.entrycget(ix, 'state')
assert state in STATES, 'unexpected state: %s' % state
iy = (STATES.index(state) + 1) % len(STATES)
ns = STATES[iy]
menu.entryconfigure(ix, state=ns)
def create_menu(app):
menu = tk.Menu(app)
app['menu'] = menu
# Rubrique <Fichier>
mm = tk.Menu(menu)
menu.add_cascade(label='Fichier', menu=mm, underline=0)
mm.add_command(label='Effacer', underline=0,
command=lambda: showinfo('Effacer'))
mm.add_command(label='Quitter', underline=0, command=app.quit)
# Rubrique <Clients>
mm = tk.Menu(menu)
menu.add_cascade(label='Client', menu=mm, underline=0)
mm.add_command(label="Animaux", underline=0,
command=lambda: showinfo("Animaux"))
mm.add_command(label="Maitres", underline=0,
command=lambda: showinfo('Maitres'))
menu.add_command(label='Toggle',
command=lambda menu=menu, label='Client': do_toggle(menu, label))
if __name__ == '__main__':
app = tk.Tk()
create_menu(app)
app.mainloop() |
Partager