Receita: Como criar menus em Tkinter
Os menus em interfaces gráficas como Tkinter podem ser facilmente manipuláveis e contruídos, veja:
Estilo clássico
1 from Tkinter import *
2 import tkFileDialog
3
4 root = Tk()
5 menubar = Menu(root)
6 root.config(menu=menubar)
7 root.title('Tk Menu')
8 root.geometry('150x150')
9
10 filemenu = Menu(menubar)
11 filemenu2 = Menu(menubar)
12 filemenu3 = Menu(menubar)
13
14 menubar.add_cascade(label='Arquivo', menu=filemenu)
15 menubar.add_cascade(label='Cores', menu=filemenu2)
16 menubar.add_cascade(label='Ajuda', menu=filemenu3)
17
18 def Open(): tkFileDialog.askopenfilename()
19 def Save(): tkFileDialog.asksaveasfilename()
20 def Quit(): root.destroy()
21 def ColorBlue(): Text(background='blue').pack()
22 def ColorRed(): Text(background='red').pack()
23 def ColorBlack(): Text(background='black').pack()
24 def Help():
25 text = Text(root)
26 text.pack();
27 text.insert('insert', 'Ao clicar no botão da\n'
28 'respectiva cor, o fundo da tela\n'
29 'aparecerá na cor escolhida.')
30
31 filemenu.add_command(label='Abrir...', command=Open)
32 filemenu.add_command(label='Salvar como...', command=Save)
33 filemenu.add_separator()
34 filemenu.add_command(label='Sair', command=Quit)
35 filemenu2.add_command(label='Azul', command=ColorBlue)
36 filemenu2.add_command(label='Vermelho', command=ColorRed)
37 filemenu2.add_command(label='Preto', command=ColorBlack)
38 filemenu3.add_command(label='Ajuda', command=Help)
39 root.mainloop()
Tabs
Volta para CookBook.