Refresh tkinter menu item label?

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

In the following example, I would like to change it from "Example Element" (in the "File" menu) to another 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() 
+6
source share
4 answers

I found the solution myself in Tcl manpages :

Use the entryconfigure() method, for example, which changes the value after it is clicked:

The first parameter 1 should be the index of the element you want to change, starting with 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() 
+9
source

I don't know if it was once different at 2.7, but it no longer works on 3.4.

On python 3.4, you should start counting entries from 0 and use entryconfig .

 menu.entryconfig(0, label = "Clicked!") 

http://effbot.org/tkinterbook/menu.htm

+2
source

Check out this dynamic menu example. The main feature here is that you do not need to worry about the serial number (index) of your menu item. Tracking does not require the index (place) of your menu. The menu item can be the first or last, it does not matter. Thus, you can add new menus without tracking (position) your menu.

The code is in Python 3.6.

 # Using lambda keyword and refresh function to create a dynamic menu. import tkinter as tk def show(x): """ Show your choice """ global label new_label = 'Choice is: ' + x menubar.entryconfigure(label, label=new_label) # change menu text label = new_label # update menu label to find it next time choice.set(x) def refresh(): """ Refresh menu contents """ global label, l if l[0] == 'one': l = ['four', 'five', 'six', 'seven'] else: l = ['one', 'two', 'three'] choice.set('') menu.delete(0, 'end') # delete previous contents of the menu menubar.entryconfigure(label, label=const_str) # change menu text label = const_str # update menu label to find it next time for i in l: menu.add_command(label=i, command=lambda x=i: show(x)) root = tk.Tk() # Set some variables choice = tk.StringVar() const_str = 'Choice' label = const_str l = ['dummy'] # Create some widgets menubar = tk.Menu(root) root.configure(menu=menubar) menu = tk.Menu(menubar, tearoff=False) menubar.add_cascade(label=label, menu=menu) b = tk.Button(root, text='Refresh menu', command=refresh) b.pack() b.invoke() tk.Label(root, textvariable=choice).pack() root.mainloop() 
0
source

OK, but can you change the entry in "menu_bar.add_cascade (label =" File ", menu = file_menu)"?

This is the problem that I have! I want the File menu to run, for example, in another language! (e.g. German to Japanese)

-1
source

Source: https://habr.com/ru/post/959307/


All Articles