python-tkinter-menu

demo
原文

1. Menubutton

1
2
3
4
5
mb = tkinter.Menubutton(
root,
bg = "red"
)
mb.pack()

2.Menu

1
2
3
4
5
6
7
8
def hello():
print ("hello ...")
m = tkinter.Menu(
root
)
m.add_command(label = "one",command = hello)
m.add_command(label = "two",command = hello)
root.config(menu = m)

3. Menu

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
def hello():
print ("hello!")
menubar = tkinter.Menu(root)
# create a pulldown menu, and add it to the menu bar
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=hello)
filemenu.add_command(label="Save", command=hello)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
# create more pulldown menus
editmenu = tkinter.Menu(menubar, tearoff=0)
editmenu.add_command(label="Cut", command=hello)
editmenu.add_command(label="Copy", command=hello)
editmenu.add_command(label="Paste", command=hello)
menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = tkinter.Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=hello)
menubar.add_cascade(label="Help", menu=helpmenu)
# display the menu
root.config(menu=menubar)

4. context menu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def hello():
print ("hello!")
# create a popup menu
menu = tkinter.Menu(root, tearoff=0)
menu.add_command(label="Undo", command=hello)
menu.add_command(label="Redo", command=hello)
# create a canvas
frame = tkinter.Frame(root, width=212, height=212)
frame.pack()
def popup(event):
menu.post(event.x_root, event.y_root)
# attach popup to canvas
frame.bind("<Button-3>", popup)

4. menu update

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
counter = 0
def update():
global counter
counter = counter + 1
menu.entryconfig(0, label=str(counter))
root = Tk()
menubar = Menu(root)
menu = Menu(menubar, tearoff=0, postcommand=update)
menu.add_command(label=str(counter))
menu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="Test", menu=menu)
root.config(menu=menubar)

5. methods

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
Menu(master=None, **options)
__init__(master=None, **options) [#]
activate(index) [#]
add(type, **options) [#]
add_cascade(**options) [#]
add_checkbutton(**options) [#]
add_command(**options) [#]
add_radiobutton(**options) [#]
add_separator(**options) [#]
config(**options) [#]
delete(index1, index2=None) [#]
entrycget(index, option) [#]
entryconfig(index, **options) [#]
entryconfigure(index, **options) [#]
index(index) [#]
insert(index, itemType, **options) [#]
insert_cascade(index, **options) [#]
insert_checkbutton(index, **options) [#]
insert_command(index, **options) [#]
insert_radiobutton(index, **options) [#]
insert_separator(index, **options) [#]
invoke(index) [#]
post(x, y) [#]
type(index) [#]
unpost() [#]
yposition(index) [#]

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. 1. Menubutton
  2. 2. 2.Menu
  3. 3. 3. Menu
  4. 4. 4. context menu
  5. 5. 4. menu update
  6. 6. 5. methods
,