472,145 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

How to change menu text with Tkinter?

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

Sep 27 '06 #1
5 4797
In <11**********************@d34g2000cwd.googlegroups .com>, Phil Schmidt
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?
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
Sep 27 '06 #2

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

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

HTH,
Rob

Sep 27 '06 #3
On Wed, 27 Sep 2006 15:29:32 +0200, Phil Schmidt
<ph*****************@yahoo.comwrote:
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+-'])"
Sep 27 '06 #4

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

Sep 27 '06 #5
Phil Schmidt wrote:
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.
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
sc***********@acm.org
Oct 1 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Adonis | last post: by
1 post views Thread by eltronic | last post: by
1 post views Thread by Martin | last post: by
1 post views Thread by Philippe C. Martin | last post: by
2 posts views Thread by gh14tq5 | last post: by
4 posts views Thread by Edward K. Ream | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.