更新tkinter菜单栏项的标签? [英] Update label of tkinter menubar item?

查看:285
本文介绍了更新tkinter菜单栏项的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用tkinter更改菜单中项目的标签?

Is it possible to change the label of an item in a menu with tkinter?

在下面的示例中,我想将其从示例项"(在文件"菜单中)更改为其他值.

In the following example, I'd like to change it from "An example item" (in the "File" menu) to a different value.

from tkinter import *

root = Tk()
menu_bar = Menu(root)

file_menu = Menu(menu_bar, tearoff=False)
file_menu.add_command(label="An example item", command=lambda: print('clicked!'))
menu_bar.add_cascade(label="File", menu=file_menu)

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

推荐答案

我自己在像这样使用entryconfigure()方法,该方法会在单击值后更改它的值:

Use the entryconfigure() method like so, which changes the value after it has been clicked:

第一个参数1必须是要更改的项目的索引,从1开始.

The first parameter 1 has to be the index of the item you want to change, starting from 1.

from tkinter import *

root = Tk()
menu_bar = Menu(root)

def clicked(menu):
    menu.entryconfigure(1, label="Clicked!")

file_menu = Menu(menu_bar, tearoff=False)
file_menu.add_command(label="An example item", command=lambda: clicked(file_menu))
menu_bar.add_cascade(label="File", menu=file_menu)

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

这篇关于更新tkinter菜单栏项的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆