我正在尝试获取菜单栏的文本(以及它的下拉菜单),以便在按下按钮时进行更改。下面的示例代码显示了我目前拥有的内容,但它无法正常工作。有人可以看一看,看看我犯的是什么错误吗?非常感谢。
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()
编辑: 我已经意识到一些错误,主要是菜单没有使用新名称重新创建,因此我尝试删除菜单,然后使用菜单标签的更新变量重新创建菜单。仍然没有工作,但我想我越来越近了:
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()
如何在按下每个按钮时删除菜单,以便以后重新创建?非常感谢你的时间。
答案 0 :(得分:2)
借助W1ll1amvl的帮助,我设法使用类似的解决方案(在stackoverflow上发布)来解决我的问题。正如您在下面看到的那样,当法语按钮'单击会重新配置每个菜单的标签文本:
#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)
我的完整代码如下:
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()