How to change menu text with Tkinter?

Eric Brunel eric_brunel at despammed.com
Wed Sep 27 10:14:14 EDT 2006


On Wed, 27 Sep 2006 15:29:32 +0200, Phil Schmidt  
<phil_nospam_schmidt at yahoo.com> wrote:

> I am making a little Tkinter GUI app that needs to be in several
> languages (english, french, etc.), adjustable at runtime via a menu
> pick to select the language. The only way I can see to change text in
> the menus entries is to destroy them and recreate them usiing different
> labels. This seems very clunky though, and there must be a better way.
> Can anyone offer any pointers or a short example for how to do this?

Here is a way:

------------------------------------------------------------------
 from Tkinter import *

root = Tk()
mb = Menu(root)
root.configure(menu=mb)
fm = Menu(mb)
mb.add_cascade(label='File', menu=fm)

def chlg():
   mb.entryconfigure(1, label='Fichier')
   fm.entryconfigure(1, label='Changer de langue')
   fm.entryconfigure(2, label='Quitter')

fm.add_command(label='Change language', command=chlg)
fm.add_command(label='Quit', command=root.quit)

root.mainloop()
------------------------------------------------------------------

Note that the entry indices start at 1 because of the 'tearoff' entry that  
is always created in a menu. If you specify the option tearoff=0 when  
creating the menus, indices will start ot 0.

But Marc's answer still applies: it's a lot of work for something that  
will usually be configured once. So requiring to restart the tool when the  
UI language changes should be acceptable.

> Thanks,
> Phil

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"



More information about the Python-list mailing list