按下按钮后可以更改菜单栏文本吗? (Python, tkinter)

Can I change the menubar text after press a button? (Python, tkinter)

我一个月前开始 python,现在我正在练习不同的东西。现在我正在构建一个计算器,并在菜单栏 上添加一个 day/night 模式开关。那很简单。但我希望我的菜单栏 在处于夜间模式时仅显示“白天模式”选项,反之亦然。 尝试了一些事情 (比如为“add_command” 使用变量,然后尝试执行“variable.config()”) 但没有成功。这样的事情能做吗?

我希望我对自己的解释足够好。对不起,如果我的英语不是很好。这是一段代码:

def mododia():
    seguro=messagebox.askquestion("Blah blah", "Blah blah blah")
    if seguro=="yes":
        File.config(background="white", fg="blue")
        Edit.config(background="white", fg="blue")
        Help.config(background="white", fg="blue")
        [...]
def modonoche():
    messagebox.showinfo("Blah blah", "Blah blah blah")
    File.config(background="black", fg="orange")
    Edit.config(background="black", fg="orange")
    Help.config(background="black", fg="orange")
    [...]
Edit=Menu(tools, tearoff=0, background="black", fg="orange")
tools.add_cascade(label="Edit", menu=Edit)
Edit.add_command(label="Modo día", command=mododia)
Edit.add_command(label="Modo noche", command=modonoche)

编辑以添加可重现的示例:

from tkinter import *
from tkinter import messagebox

root=Tk()
root.resizable(width=0, height=0)

def mododia():
    seguro=messagebox.askquestion("Activar modo día", "Se recomienda no utilizar este modo con poca luz. ¿Está seguro de querer activar el modo día?")
    if seguro=="yes":
        File.config(background="white", fg="blue")
        Edit.config(background="white", fg="blue")
        Help.config(background="white", fg="blue")
        frame1.config(bg="white")
        frame2.config(bg="white")
        previous.config(bg="white")
        previouseq.config(bg="white")
        display.config(bg="white", fg="black")
        current.config(bg="white")
        butclear.config(bg="white", fg="blue")
        butdelete.config(bg="white", fg="blue")
        butpercen.config(bg="white", fg="blue")
        butdivide.config(bg="white", fg="blue")
        but7.config(bg="white", fg="black")
        but8.config(bg="white", fg="black")
        but9.config(bg="white", fg="black")
        butmulti.config(bg="white", fg="blue")
        but4.config(bg="white", fg="black")
        but5.config(bg="white", fg="black")
        but6.config(bg="white", fg="black")
        butsubs.config(bg="white", fg="blue")
        but1.config(bg="white", fg="black")
        but2.config(bg="white", fg="black")
        but3.config(bg="white", fg="black")
        butadd.config(bg="white", fg="blue")
        butpotency.config(bg="white", fg="blue")
        but0.config(bg="white", fg="black")
        butdot.config(bg="white", fg="black")
        butequal.config(bg="blue", fg="white")
        root.config(bg="white")

def modonoche():
    messagebox.showinfo("Activar modo noche", "Esta es la configuración recomendada por el creador del programa.")
    File.config(background="black", fg="orange")
    Edit.config(background="black", fg="orange")
    Help.config(background="black", fg="orange")
    frame1.config(bg="black")
    frame2.config(bg="black")
    previous.config(bg="black")
    previouseq.config(bg="black")
    display.config(bg="black", fg="white")
    current.config(bg="black")
    butclear.config(bg="black", fg="orange")
    butdelete.config(bg="black", fg="orange")
    butpercen.config(bg="black", fg="orange")
    butdivide.config(bg="black", fg="orange")
    but7.config(bg="black", fg="white")
    but8.config(bg="black", fg="white")
    but9.config(bg="black", fg="white")
    butmulti.config(bg="black", fg="orange")
    but4.config(bg="black", fg="white")
    but5.config(bg="black", fg="white")
    but6.config(bg="black", fg="white")
    butsubs.config(bg="black", fg="orange")
    but1.config(bg="black", fg="white")
    but2.config(bg="black", fg="white")
    but3.config(bg="black", fg="white")
    butadd.config(bg="black", fg="orange")
    butpotency.config(bg="black", fg="orange")
    but0.config(bg="black", fg="white")
    butdot.config(bg="black", fg="white")
    butequal.config(bg="orange", fg="white")
    root.config(bg="black")

def helpme():
    ayuda=messagebox.showinfo("Ayuda", "Pulse las cifras con las que desea operar y las operaciones a realizar como en una calculadora corriente.")

def about():
    acercade=messagebox.showinfo("Calculadora 1.0", "Primera versión de calculadora creada por Shinington.")

def exit():
    salir=messagebox.askquestion("Cerrar calculadora", "¿Seguro que desea salir?")
    if salir=="yes":
        root.destroy()

def erase():
    inputdis.set(0)
    inputcurr.set(0)

def new():
    erase()
    inputprev.set(0)
    inputpreq.set(0)


tools=Menu(root)
File=Menu(tools, tearoff=0, background="black", fg="orange")
tools.add_cascade(label="File", menu=File)
File.add_command(label="Nuevo", command=new)
File.add_command(label="Borrar", command=erase)
File.add_separator()
File.add_command(label="Salir", command=exit)
Edit=Menu(tools, tearoff=0, background="black", fg="orange")
tools.add_cascade(label="Edit", menu=Edit)
Edit.add_command(label="Modo día", command=mododia)
Edit.add_command(label="Modo noche", command=modonoche)
Help=Menu(tools, tearoff=0, background="black", fg="orange")
tools.add_cascade(label="Help", menu=Help)
Help.add_command(label="Ayuda", command=helpme)
Help.add_command(label="Acerca de...", command=about)
root.config(bg="black", menu=tools)

frame1=Frame(root)
frame1.pack()
frame2=Frame(root)
frame2.pack()
frame1.config(bg="black", width=40, height=30)
frame2.config(bg="black", width=40, height=60)

inputdis=IntVar()
inputprev=IntVar()
inputpreq=IntVar()
inputcurr=IntVar()
previous=Entry(frame1, textvariable=inputprev)
previous.grid(row=0, column=0, sticky="e")
previous.config(fg="gray", bg="black", font=("", 8), width=40, justify="right", borderwidth=0)
previouseq=Entry(frame1, textvariable=inputpreq)
previouseq.grid(row=1, column=0, sticky="e")
previouseq.config(fg="gray", bg="black", font=("", 8), width=40, justify="right", borderwidth=0)
display=Entry(frame1, textvariable=inputdis)
display.grid(row=2, column=0, pady=10, sticky="e")
display.config(fg="white", bg="black", font=("", 12, "bold"), width=30, justify="right", borderwidth=0)
current=Entry(frame1, textvariable=inputcurr)
current.grid(row=3, column=0, pady=10, sticky="e")
current.config(fg="gray", bg="black", font=("", 8), width=40, justify="right", borderwidth=0)

butclear=Button(frame2, text="AC", width=7)
butclear.grid(row=0, column=0, padx=5, pady=10)
butclear.config(fg="orange", bg="black", font=("", 9, "bold"), borderwidth=0)
butdelete=Button(frame2, text="DEL", width=7)
butdelete.grid(row=0, column=1, padx=5, pady=10)
butdelete.config(fg="orange", bg="black", font=("", 9, "bold"), borderwidth=0)
butpercen=Button(frame2, text="%", width=7)
butpercen.grid(row=0, column=2, padx=5, pady=10)
butpercen.config(fg="orange", bg="black", font=("", 10, "bold"), borderwidth=0)
butdivide=Button(frame2, text=r"/", width=7)
butdivide.grid(row=0, column=3, padx=5, pady=10)
butdivide.config(fg="orange", bg="black", font=("", 10, "bold"), borderwidth=0)

but7=Button(frame2, text="7", width=7)
but7.grid(row=1, column=0, padx=5, pady=10)
but7.config(fg="white", bg="black", font=("", 10, "bold"), borderwidth=0)
but8=Button(frame2, text="8", width=7)
but8.grid(row=1, column=1, padx=5, pady=10)
but8.config(fg="white", bg="black", font=("", 10, "bold"), borderwidth=0)
but9=Button(frame2, text="9", width=7)
but9.grid(row=1, column=2, padx=5, pady=10)
but9.config(fg="white", bg="black", font=("", 10, "bold"), borderwidth=0)
butmulti=Button(frame2, text="x", width=7)
butmulti.grid(row=1, column=3, padx=5, pady=10)
butmulti.config(fg="orange", bg="black", font=("", 10, "bold"), borderwidth=0)

but4=Button(frame2, text="4", width=7)
but4.grid(row=2, column=0, padx=5, pady=10)
but4.config(fg="white", bg="black", font=("", 10, "bold"), borderwidth=0)
but5=Button(frame2, text="5", width=7)
but5.grid(row=2, column=1, padx=5, pady=10)
but5.config(fg="white", bg="black", font=("", 10, "bold"), borderwidth=0)
but6=Button(frame2, text="6", width=7)
but6.grid(row=2, column=2, padx=5, pady=10)
but6.config(fg="white", bg="black", font=("", 10, "bold"), borderwidth=0)
butsubs=Button(frame2, text="-", width=7)
butsubs.grid(row=2, column=3, padx=5, pady=10)
butsubs.config(fg="orange", bg="black", font=("", 10, "bold"), borderwidth=0)

but1=Button(frame2, text="1", width=7)
but1.grid(row=3, column=0, padx=5, pady=10)
but1.config(fg="white", bg="black", font=("", 10, "bold"), borderwidth=0)
but2=Button(frame2, text="2", width=7)
but2.grid(row=3, column=1, padx=5, pady=10)
but2.config(fg="white", bg="black", font=("", 10, "bold"), borderwidth=0)
but3=Button(frame2, text="3", width=7)
but3.grid(row=3, column=2, padx=5, pady=10)
but3.config(fg="white", bg="black", font=("", 10, "bold"), borderwidth=0)
butadd=Button(frame2, text="+", width=7)
butadd.grid(row=3, column=3, padx=5, pady=10)
butadd.config(fg="orange", bg="black", font=("", 10, "bold"), borderwidth=0)

butpotency=Button(frame2, text="x^", width=7)
butpotency.grid(row=4, column=0, padx=5, pady=10)
butpotency.config(fg="orange", bg="black", font=("", 10, "bold"), borderwidth=0)
but0=Button(frame2, text="0", width=7)
but0.grid(row=4, column=1, padx=5, pady=10)
but0.config(fg="white", bg="black", font=("", 10, "bold"), borderwidth=0)
butdot=Button(frame2, text=".", width=7)
butdot.grid(row=4, column=2, padx=5, pady=10)
butdot.config(fg="white", bg="black", font=("", 10, "bold"), borderwidth=0)
butequal=Button(frame2, text="=", width=7)
butequal.grid(row=4, column=3, padx=5, pady=10)
butequal.config(fg="white", bg="orange", font=("", 10, "bold"), borderwidth=0)


root.mainloop()

您可以在“modo día”(白天模式)和“modo noche”(夜间模式)之间切换,但我希望菜单栏上只有一个选项可用(白天时为夜间模式)选择模式,反之亦然)。

我不希望它随着时间的推移自动改变,而是通过菜单栏手动改变它。这可以用 Bryan Oakley 谈到的 entryconfigure 来完成吗?我也不知道关于 Flags Cool Cloud 的提及。正如我所说,几周前我就开始编程了。

删除这一行:

Edit.add_command(label="Modo noche", command=modonoche)

将这两行添加到您的代码中:

def modonoche():
    ...
    Edit.entryconfigure(0,label='mododia',command=mododia)

def mododia():
    seguro=messagebox.askquestion("Activar modo día", "Se recomienda no utilizar este modo con poca luz. ¿Está seguro de querer activar el modo día?")
    if seguro=="yes":
        ...
        Edit.entryconfigure(0,label='modonoche',command=modonoche)

来源: http://tcl.tk/man/tcl8.5/TkCmd/menu.htm#M55