Oliver
Oliver

Reputation: 33

How can i change Text inside a menu during program is running?

I want to give the user the chance to change the language from german to japanese while the program is running! (Multi language program) I did my best and checked out manything, but i could not find a way!

Here is the code:

# -*- coding: utf-8 -*-

from tkinter import *

lang_datei = ("Datei", "Neu", "Beenden")
lang_sprache = ("Language", "Deutsch", "日本語")

fenster = Tk()
fenster.geometry("500x400")

# Definition Text widget
def buildnew_textwidget():
    T.insert(END, "")
T = Text(fenster, height=500, width=400)
T.pack()

### Definitionen filemenu ###
def NewFile():
    pass

### Definition String Language DE / JP
def Sprache_de():
    lang_datei = ("Datei", "Neu", "Beenden")
    lang_sprache = ("Sprache", "Deutsch", "日本語")

def Sprache_jp():
    lang_datei = ("ファイル", "新しい", "終了する")
    lang_sprache = ("言語", "Deutsch", "日本語")

menu = Menu(fenster)

# file menu
filemenu = Menu(menu, bd=0, tearoff=0,)
menu.add_cascade(label=lang_datei[0], menu=filemenu)
filemenu.add_command(label=lang_datei[1], command=NewFile)
filemenu.add_command(label=lang_datei[2], command=fenster.quit)

# language menu
sprachmenu = Menu(menu, tearoff=0)
menu.add_cascade(label=lang_sprache[0], menu=sprachmenu)
sprachmenu.add_command(label=lang_sprache[1], command=Sprache_de)
sprachmenu.add_command(label=lang_sprache[2], command=Sprache_jp)


fenster.config(menu=menu)
fenster.mainloop()

Upvotes: 0

Views: 188

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

To change the text of an existing menu item you must use the entryconfigure method. It takes as an argument an index (numerical index, or the text of an existing item), and then one or more options and their new values.

For example, to change the label of the first item in a menu to "Hello", you would do something like this:

the_menu.entryconfigure(0, label="Hello")

Upvotes: 1

Related Questions