How to change menu text with Tkinter?

P

Phil Schmidt

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?

Thanks,
Phil
 
M

Marc 'BlackJack' Rintsch

Phil Schmidt said:
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?

Do you really need this at runtime or is it enough to store the language
in a config file and ask the user to restart the application? Then using
the `gettext` module is a good way. Maybe in combination with the
`locale` module so you can use the language of the operating system as
default if the user doesn't choose one explicitly.

Ciao,
Marc 'BlackJack' Rintsch
 
R

Rob Wolfe

Phil said:
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?

Try this:

menu.entryconfig(index, label="new_name")

HTH,
Rob
 
E

Eric Brunel

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
 
P

Phil Schmidt

Eric Brunel wrote:

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 for the example, that helps.

I agree with you and Marc regarding the language configuration method.
The requirements aren't mine however - my customer wants the language
selectable at runtime, so I'm kind of stuck with that.

Phil
 
S

Scott David Daniels

Phil said:
Thanks for the example, that helps.

I agree with you and Marc regarding the language configuration method.
The requirements aren't mine however - my customer wants the language
selectable at runtime, so I'm kind of stuck with that.

You might also explain to the customer that of any menu elements are
ordered alphabetically, the result of changing the language will be
jarring to the user, as well as expensive to implement.

--Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top