Python tkinter--Capítulo 16 Menú (Menú) Método

16.2 Métodos

método describir
:add(tipo, **opciones) Agregar un elemento de menú.
Los valores opcionales de Tipo incluyen: "comando", "cascada" (submenú), "botón de verificación", "botón de radio", "separador"
add_cascade(**opciones) Agregar un elemento del menú en cascada
add_checkbutton(**opciones) Agregue un elemento de menú con un botón de verificación (checkbutton)
add_command(**opciones) Agregar un elemento de menú de comando (comando)
add_radiobutton(**opciones) Agregar un elemento de menú con un botón de opción
add_separator(**opciones) Agregar un separador
config(**opciones) Configurar las propiedades del menú. Vea la descripción del atributo del menú para más detalles.
borrar(índice1, índice2=Ninguno) Elimine uno o más elementos del menú.
index1: el valor inicial del elemento de menú que se eliminará
index2: el valor final del elemento de menú que se eliminará. Si no se establece este valor, solo se eliminará el elemento de menú especificado por index1.
entrycget(índice, opción) Obtener las propiedades de los elementos del menú
entryconfig(índice, **opciones)
entryconfigure(índice, **opciones)
Cambiar las propiedades de los elementos del menú
índice(índice) Convierta el índice del elemento de menú en un número entero, independientemente del tipo de índice especificado.
insertar (índice, tipo de artículo, ** opciones) Inserta un elemento de menú del tipo especificado.
insert_cascade(índice, **opciones) Agregar submenús (menús en cascada).
insert_checkbutton(índice, **opciones) Se agregó el menú del botón de verificación (botón de verificación).
insert_command(índice, **opciones) Agregar menú de comando (comando)
insert_radiobutton(índice, **opciones) Agregar menú de botón de radio (botón de radio).
insert_separator(índice, **opciones) Añadir divisor.
invocar(índice) Llame a la función de elemento de menú indexada por índice
puesto(x,y) Mostrar el menú emergente en la posición especificada por las coordenadas (x,y)
tipo (índice) Devuelve el tipo del elemento de menú especificado.
no publicar () Eliminar el menú publicado
posición(índice) Devuelve el desplazamiento vertical del elemento de menú especificado por índice. Se utiliza en los menús emergentes.
**16.2.1 añadir( tipo de elemento, cnf={}, kw):
Según los comentarios en el código fuente, esta es una función interna, pero también se puede usar. Debido a consideraciones de portabilidad, no se recomienda el uso de esta función.
Los tipos de itemType son:
(1) cascada
(2) botón de verificación
(3) comando
4)botón de radio
(5) separador

相应的类型都有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位置的垂直坐标。

Acho que você gosta

Origin blog.csdn.net/weixin_42272768/article/details/100808991
Recomendado
Clasificación