Python tkinter--Chapter 16 Menu (Menu) method

16.2 Methods

method describe
:add(type, **options) Add a menu item.
The optional values ​​​​of Type include: "command", "cascade" (submenu), "checkbutton", "radiobutton", "separator"
add_cascade(**options) Add a cascade menu item
add_checkbutton(**options) Add a menu item with a check button (checkbutton)
add_command(**options) Add a command (command) menu item
add_radiobutton(**options) Add a menu item with a radio button
add_separator(**options) Add a separator
config(**options) Configure menu properties. For details, see the menu attribute description.
delete(index1, index2=None) Delete one or more menu items.
index1: the start value of the menu item to be deleted
index2: the end value of the menu item to be deleted. If this value is not set, only the menu item specified by index1 will be deleted.
entrycget(index, option) Get menu item properties
entryconfig(index, **options)
entryconfigure(index, **options)
Change menu item properties
index(index) Convert the index of the menu item to an integer, regardless of the type of the specified index.
insert(index, itemType, **options) Inserts a menu item of the specified type.
insert_cascade(index, **options) Add submenus (cascading menus).
insert_checkbutton(index, **options) Added check button menu (checkbutton).
insert_command(index, **options) Add command menu (command)
insert_radiobutton(index, **options) Add radio button menu (radiobutton).
insert_separator(index, **options) Add divider.
invoke(index) Call the menu item function indexed by index
post(x,y) Display the popup menu at the position specified by coordinates (x,y)
type(index) Returns the type of the specified menu item.
unpost() Remove the posted menu
yposition(index) Returns the vertical offset of the menu item specified by index. Used in popup menus.
**16.2.1 add( itemType, cnf={}, kw):
According to the comments in the source code, this is an internal function, but it can also be used. Due to portability considerations, the use of this function is not recommended.
The types of itemType are:
(1)cascade
(2)checkbutton
(3)command
4)radiobutton
(5)separator

相应的类型都有add_xxx函数。比如add_cascade等。
**16.2.2 add_cascade(cnf={}, kw)
增加一个层叠菜单,就是子菜单。比如前面的例子就使用了这个方法。
menubar.add_cascade(label=‘文件’, menu=filemenu)
此代码就是把filemenu定义的菜单,作为层叠菜单加入到menubar定义的顶层菜单之中。
**16.2.3 add_checkbutton(cnf={}, kw)
添加一个checkbutton类型的菜单项。
filemenu.add_checkbutton(label=‘打开文件’, command=open_file, variable=openVar)
checkbutton 类型的菜单项可以多选,就是可以有多个checkbutton类型的菜单项被选中。这个是与radiobutton菜单项的区别。radiobutton菜单项只能有一个被选中。
checkbutton 菜单项被选中后,会在文本标识的前面出现一个’√’。再次点击该菜单项,则’√’消失,表示不再被选中。
在这里插入图片描述
**16.2.4 add_command(cnf={}, kw)
增加一个命令菜单项。选项有:
(1)label
使用文本标签
(2)bitmap
使用图像,早期的bitmap类型的。现在已经几乎不使用了。
(3)image
使用图片作为菜单项标识。
(4)command
菜单项的回调函数。点击菜单并释放按键后,会调用这个函数

具体例子见16.1的说明
**16.2.5 add_radiobutton(cnf={}, kw)
增加一个radiobutton菜单项。Radiobutton与checkbutton非常类似,只不过radiobutton同时只能选中一组菜单项中的一个,而checkbutton可以选中多个。
**16.2.6 add_separator(cnf={}, kw)
增加一个分隔条。就是一条水平的线,把菜单命令分成不同组。
16.2.7 config(cnf={}, **kw)
动态修改菜单项的属性。具体属性说明见16.1。另外不是所有的属性都可以动态修改。
16.2.8 delete(index1, index2=None)
删除菜单项。index1是起始的菜单项,而index2是结束的菜单项。如果没有输入index2,就只删除index1代表的一个菜单项。注意:删除是不包括index2代表的菜单项的。

import tkinter as tk
from tkinter import ttk

root=tk.Tk()
root.geometry('320x240')
b1=tk.Label()
b1.pack()
def open_file():
    b1['text']='打开文件'
def close_file():
    b1['text']='关闭文件'

menubar = tk.Menu(root)
filemenu=tk.Menu(menubar)

filemenu.add_command(label='打开文件',command=open_file)
filemenu.add_command(label='关闭文件',command=close_file)
filemenu.add_separator()
filemenu.add_command(label='测试1')
filemenu.add_command(label='测试2')
filemenu.add_command(label='测试3')
filemenu.add_command(label='测试4')
menubar.add_cascade(label='文件', menu=filemenu)
menubar.add_command(label='退出',command=root.destroy)

def delete():
    filemenu.delete(3,7)

b3=tk.Button(root,text='Delete',command=delete)
b3.pack(side='left')

root.config(menu=menubar)
root.mainloop()

16.2.9 entrycget(index, option)
获取index菜单项的属性。注意,这里获取的是用add_xxx添加的菜单项,而不是用tk.Menu声明的菜单。

import tkinter as tk
from tkinter import ttk

root=tk.Tk()
root.geometry('320x240')
b1=tk.Label()
b1.pack()
def open_file():
    b1['text']='打开文件'
def close_file():
    b1['text']='关闭文件'

menubar = tk.Menu(root)
filemenu=tk.Menu(menubar)

filemenu.add_command(label='打开文件',command=open_file)
filemenu.add_command(label='关闭文件',command=close_file)
menubar.add_cascade(label='文件', menu=filemenu)
menubar.add_command(label='退出',command=root.destroy)

def cget():
    b1['text']=filemenu.entrycget(1,'label')

b3=tk.Button(root,text='CGet',command=cget)
b3.pack(side='left')

root.config(menu=menubar)
root.mainloop()

**16.2.10 entryconfigure(index, cnf=None, kw)
更改菜单项属性。主要用于动态修改菜单。

import tkinter as tk
from tkinter import ttk

root=tk.Tk()
root.geometry('320x240')
b1=tk.Label()
b1.pack()
def open_file():
    b1['text']='打开文件'
def close_file():
    b1['text']='关闭文件'
def saveas_file():
    b1['text']='另存为'
    
menubar = tk.Menu(root)
filemenu=tk.Menu(menubar)

filemenu.add_command(label='打开文件',command=open_file)
filemenu.add_command(label='关闭文件',command=close_file)
menubar.add_cascade(label='文件', menu=filemenu)
menubar.add_command(label='退出',command=root.destroy)


def config():
    filemenu.entryconfigure(1,label='另存为',command=saveas_file)

b3=tk.Button(root,text='Config',command=config)
b3.pack(side='left')

root.config(menu=menubar)
root.mainloop()

entryconfig 与entryconfigure的作用是一样的。
16.2.11 index(index)
返回index的整数形式位置值或者索引值。注意计数是从0开始,tearoff和separator都算占用位置的。index可以是tk.END等非数字形式的索引。
**16.2.12 insert( index, itemType, cnf={}, kw)
insert是个内部函数。类似于前面的add()函数。请参考16.2.1节的说明
**16.2.13 insert_cascade(index, cnf={}, kw)
在指定位置插入一个层叠菜单。这个函数与add_cascade()类似,不过add_cascade()只能顺序添加,而insert_cascade()支持在指定的位置添加。

import tkinter as tk
from tkinter import ttk

root=tk.Tk()
root.geometry('320x240')
b1=tk.Label()
b1.pack()
def open_file():
    b1['text']='打开文件'
def close_file():
    b1['text']='关闭文件'
def saveas_file():
    b1['text']='另存为' 
menubar = tk.Menu(root)
filemenu=tk.Menu(menubar)
c=tk.Menu(menubar)
c.add_command(label='另存为',command=saveas_file)
filemenu.add_command(label='打开文件',command=open_file)
filemenu.add_command(label='关闭文件',command=close_file)
menubar.add_cascade(label='文件', menu=filemenu)
menubar.add_command(label='退出',command=root.destroy)
def insert_c():
    filemenu.insert_cascade(index=2,label='插入',menu=c)

b3=tk.Button(root,text='Insert_Cascade',command=insert_c)
b3.pack(side='left')

root.config(menu=menubar)
root.mainloop()

结果:
在这里插入图片描述
在这里插入图片描述
**16.2.14 insert_checkbutton( index, cnf={}, kw)
在指定位置插入一个checkbutton菜单项。用法类似add_checkbutton(),见16.2.3。
**16.2.15 insert_command(index, cnf={}, kw)
在指定位置插入一个命令菜单项。用法类似add_command(),见16.2.4
**16.2.16 insert_radiobutton( index, cnf={}, kw)
在指定位置插入一个radiobutton菜单项。用法类似add_radiobutton(),见16.2.5
**16.2.17 insert_separator(index, cnf={}, kw)
在指定位置插入一个separator菜单项。用法类似add_separator(),见16.2.6
16.2.18 invoke(index)
调用index指定位置的菜单项的回调函数。等同于点击了该菜单项。
比如:filemenu.invoke(1)
16.2.19 post( x, y)
在坐标(x,y)处弹出菜单,就是实现弹出式菜单。一般是点击右键。由于(x,y)的坐标是屏幕的坐标,因此需要先取得窗口的坐标,再和event中的鼠标坐标相加,就是窗口的坐标到屏幕的坐标的变换。

import tkinter as tk
from tkinter import ttk

root=tk.Tk()
root.geometry('320x240')
b1=tk.Label()
b1.pack()
def open_file():
    b1['text']='打开文件'
def close_file():
    b1['text']='关闭文件'

filemenu=tk.Menu(root)
filemenu.add_command(label='打开文件',command=open_file)
filemenu.add_command(label='关闭文件',command=close_file)

def pop_up(event):
    root.update()
    x= root.winfo_rootx()
    y=root.winfo_rooty()
    filemenu.post(x+event.x,y+event.y)

root.bind('<Button-3>',pop_up)    
root.mainloop()

结果:
在这里插入图片描述
16.2.20 type(index)
返回指定菜单项的类型:
(1)Cascade
(2)command
(3)checkbutton
(4)radiobutton
(5)seperator

比如:filemenu.type(1)
16.2.21 yposition(index)
返回index位置的垂直坐标。

Guess you like

Origin blog.csdn.net/weixin_42272768/article/details/100808991