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.