Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tkinter 在单击按钮时获取要更改的菜单栏文本。为什么我的变量没有更新?_Tkinter - Fatal编程技术网

Tkinter 在单击按钮时获取要更改的菜单栏文本。为什么我的变量没有更新?

Tkinter 在单击按钮时获取要更改的菜单栏文本。为什么我的变量没有更新?,tkinter,Tkinter,我试图让一个菜单栏(及其下拉菜单)的文本在按下按钮时发生变化。下面的示例代码显示了我当前拥有的内容,但它不起作用。有人能看看我犯了什么错误吗?非常感谢 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():

我试图让一个菜单栏(及其下拉菜单)的文本在按下按钮时发生变化。下面的示例代码显示了我当前拥有的内容,但它不起作用。有人能看看我犯了什么错误吗?非常感谢

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()

如何在按下每个按钮时删除菜单,以便以后可以重新创建?非常感谢您的时间。

在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()

看到这个,它应该会给你一些帮助。太棒了-谢谢你!干得好,现在很好用。我想你可以自己做:-)。