0

I am trying to get the text of a menu bar (and it's drop downs) to change when a button is pressed. The example code below shows what I currently have but it is not working. Could someone take a look and see what mistake I am making? Many thanks.

from tkinter import *

m_label1="Menu 1"
dd1_label1="Option 1"
dd1_label2="Option 2"
m_label2 = "Menu 2"
dd2_label1 = "Option 1"
dd2_label2 = "Option 2"

def function1():
    textarea.delete(0.0, END)
    textarea.insert(END, "You have selected menu option 1")

def function2():
    textarea.delete(0.0, END)
    textarea.insert(END, "Now you have selected menu option 2")

def englishmenu():
    global m_label1
    global dd1_label1
    global dd1_label2
    global m_label2
    global dd2_label1
    global dd2_label2
    m_label1 = "Menu 1"
    dd1_label1 = "Option 1"
    dd1_label2 = "Option 2"
    m_label2 = "Menu 2"
    dd2_label1 = "Option 1"
    dd2_label2 = "Option 2"

def frenchmenu():
    global m_label1
    global dd1_label1
    global dd1_label2
    global m_label2
    global dd2_label1
    global dd2_label2
    m_label1 = "Carte 1"
    dd1_label1 = "Choix 1"
    dd1_label2 = "Choix 2"
    m_label2 = "Carte 2"
    dd2_label1 = "Choix 1"
    dd2_label2 = "Choix 2"

window = Tk()
window.geometry("290x220")

# create a toplevel menu
menubar = Menu(window)

firstmenu = Menu(menubar, tearoff=0)

firstmenu.add_command(label=dd1_label1, command=function1)
firstmenu.add_command(label=dd1_label2, command=window.destroy)
menubar.add_cascade(label=m_label1, menu=firstmenu)

secondmenu = Menu(menubar, tearoff=0)

secondmenu.add_command(label=dd2_label1, command=function2)
secondmenu.add_command(label=dd2_label2, command=window.destroy)
menubar.add_cascade(label=m_label2, menu=secondmenu)

window.config(menu=menubar)

#textbox
textarea = Text(window, width=35, height=10, wrap=WORD, bg="lightblue")
textarea.grid(row=0, column=0, sticky = W)

english = Button(window, width = 5, text="English", command=englishmenu)
english.grid(row=1, column=0, sticky=W)

french = Button(window, width = 5, text="French", command=frenchmenu)
french.grid(row=2, column=0, sticky=W)

window.mainloop()

EDIT: I have realised a few mistakes, mainly that the menu wasn't being recreated with the new names so I have tried to delete the menu and then re-create it with updated variables for the menu labels. Still not working but I think I am getting closer:

from tkinter import *

m_label1="Menu 1"
dd1_label1="Option 1"
dd1_label2="Option 2"
m_label2 = "Menu 2"
dd2_label1 = "Option 1"
dd2_label2 = "Option 2"

def function1():
    textarea.delete(0.0, END)
    textarea.insert(END, "You have selected menu option 1")

def function2():
    textarea.delete(0.0, END)
    textarea.insert(END, "Now you have selected menu option 2")

def englishmenu():
    global m_label1
    global dd1_label1
    global dd1_label2
    global m_label2
    global dd2_label1
    global dd2_label2
    m_label1 = "Menu 1"
    dd1_label1 = "Option 1"
    dd1_label2 = "Option 2"
    m_label2 = "Menu 2"
    dd2_label1 = "Option 1"
    dd2_label2 = "Option 2"
    menubar.delete()
    menucreate()


def frenchmenu():
    global m_label1
    global dd1_label1
    global dd1_label2
    global m_label2
    global dd2_label1
    global dd2_label2
    m_label1 = "Carte 1"
    dd1_label1 = "Choix 1"
    dd1_label2 = "Choix 2"
    m_label2 = "Carte 2"
    dd2_label1 = "Choix 1"
    dd2_label2 = "Choix 2"
    menubar.delete("all")
    menucreate()

def menucreate():
    global menubar
    global firstmenu
    global secondmenu

    menubar = Menu(window)

    firstmenu = Menu(menubar, tearoff=0)

    firstmenu.add_command(label=dd1_label1, command=function1)
    firstmenu.add_command(label=dd1_label2, command=window.destroy)
    menubar.add_cascade(label=m_label1, menu=firstmenu)

    secondmenu = Menu(menubar, tearoff=0)

    secondmenu.add_command(label=dd2_label1, command=function2)
    secondmenu.add_command(label=dd2_label2, command=window.destroy)
    menubar.add_cascade(label=m_label2, menu=secondmenu)

    window.config(menu=menubar)

window = Tk()
window.geometry("290x220")

menucreate()

#textbox
textarea = Text(window, width=35, height=10, wrap=WORD, bg="lightblue")
textarea.grid(row=0, column=0, sticky = W)

english = Button(window, width = 5, text="English", command=englishmenu)
english.grid(row=1, column=0, sticky=W)

french = Button(window, width = 5, text="French", command=frenchmenu)
french.grid(row=2, column=0, sticky=W)

window.mainloop()

How can i get the menu deleted when each button is pressed so that it can later be recreated? Many thanks for your time.

sw123456
  • 3,339
  • 1
  • 24
  • 42
  • 1
    see this, it should give you a bit of help. http://stackoverflow.com/questions/20369754/update-label-of-tkinter-menubar-item – W1ll1amvl Oct 09 '14 at 06:15

1 Answers1

2

With W1ll1amvl's help I managed to use a similar solution (posted on stackoverflow) to solve my problem. As you can see below, the function called when the 'french button' is clicked reconfigures the label text for each menu:

#function to change text
def frenchmenu():
    menubar.entryconfigure(1, label="Carte 1")
    firstmenu.entryconfigure(0, label="Choix 1")
    firstmenu.entryconfigure(1, label="Choix 2")
    menubar.entryconfigure(2, label="Carte 2")
    secondmenu.entryconfigure(0, label="Choix 1")
    secondmenu.entryconfigure(1, label="Choix 2")

#button code
french = Button(window, width = 5, text="French", command=frenchmenu)
french.grid(row=2, column=0, sticky=W)

My full code is below:

from tkinter import *

def function1():
    textarea.delete(0.0, END)
    textarea.insert(END, "You have selected menu option 1")

def function2():
    textarea.delete(0.0, END)
    textarea.insert(END, "Now you have selected menu option 2")

def englishmenu():
    menubar.entryconfigure(1, label="Menu 1")
    firstmenu.entryconfigure(0, label="Option 1")
    firstmenu.entryconfigure(1, label="Option 2")
    menubar.entryconfigure(2, label="Menu 2")
    secondmenu.entryconfigure(0, label="Option 1")
    secondmenu.entryconfigure(1, label="Option 2")


def frenchmenu():
    menubar.entryconfigure(1, label="Carte 1")
    firstmenu.entryconfigure(0, label="Choix 1")
    firstmenu.entryconfigure(1, label="Choix 2")
    menubar.entryconfigure(2, label="Carte 2")
    secondmenu.entryconfigure(0, label="Choix 1")
    secondmenu.entryconfigure(1, label="Choix 2")


window = Tk()
window.geometry("290x220")

#create menu
menubar = Menu(window)

firstmenu = Menu(menubar, tearoff=0)

firstmenu.add_command(label="Option 1", command=function1)
firstmenu.add_command(label="Option 2", command=window.destroy)
menubar.add_cascade(label="Menu 1", menu=firstmenu)

secondmenu = Menu(menubar, tearoff=0)

secondmenu.add_command(label="Option 1", command=function2)
secondmenu.add_command(label="Option 2", command=window.destroy)
menubar.add_cascade(label="Menu 2", menu=secondmenu)

window.config(menu=menubar)

#textbox
textarea = Text(window, width=35, height=10, wrap=WORD, bg="lightblue")
textarea.grid(row=0, column=0, sticky = W)

english = Button(window, width = 5, text="English", command=englishmenu)
english.grid(row=1, column=0, sticky=W)

french = Button(window, width = 5, text="French", command=frenchmenu)
french.grid(row=2, column=0, sticky=W)

window.mainloop()
sw123456
  • 3,339
  • 1
  • 24
  • 42