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

Posted

技术标签:

【中文标题】按下按钮后可以更改菜单栏文本吗? (Python,tkinter)【英文标题】:Can I change the menubar text after press a button? (Python, tkinter) 【发布时间】:2021-10-03 18:03:07 【问题描述】:

我一个月前开始使用 python,并且正在练习不同的东西。现在我正在构建一个计算器,并在菜单栏上添加一个日/夜模式开关。那很简单。但我希望我的菜单栏在夜间模式下仅显示“白天模式”选项,反之亦然。 尝试了几件事(例如为“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 的提及。正如我所说,我几周前就开始编程了。

【问题讨论】:

你可以使用flags 请提供minimal reproducible example。此外,您尝试过什么来实现这一目标?你想按时间设置吗?或者你想通过太阳路线设置它?您的环境如何或应该如何实施?请在提问之前花点时间解决您的问题。 菜单有一个记录在案的entryconfigure 方法。你试过用吗? 我不知道菜单有入口配置,我怎么能用它来解决这个问题?另外,也不知道标志是什么。正如我所说,我刚刚开始这样做,所以我还有很多东西要学习,但我没有在互联网上找到答案。编辑添加了一个可重现的示例@Atlas435 感谢您的宝贵时间 【参考方案1】:

删除这一行:

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

【讨论】:

以上是关于按下按钮后可以更改菜单栏文本吗? (Python,tkinter)的主要内容,如果未能解决你的问题,请参考以下文章

动作栏文字颜色

按下网页按钮时更改iOS标签栏项目名称

按下按钮更改标签文本

无法更改后退栏按钮文本

按下后退按钮后,从另一个视图控制器更改按钮

ReactNative JS 文本未更新