I started with python a month ago and I'm practicing different stuff. Right now I'm building a calculator and I'm adding a day/night mode switch on menubar. That was easy. But I'd like my menubar to show only "day mode" option when it's on night mode, and viceversa. Tried a couple of things (like use a variable for the "add_command" and then try to do "variable.config()") but didn't work. Can such thing be done?
I hope I've explained myself good enough. Sorry if my english is not quite good. Here is a piece of the code:
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)
Edit to add a reproducible example:
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()
There you can switch between "modo día" (day mode) and "modo noche" (night mode), but I'd like that only one of the options is available on the menu bar (night mode when day mode is selected and viceversa).
I don't want it to change automatically as time goes on, but to manually change it with the menubar. Can that be done with the entryconfigure that Bryan Oakley talks about? I don't know about the Flags Cool Cloud mention either. As I said, I've started programming a few weeks ago.
CodePudding user response:
Delete this line:
Edit.add_command(label="Modo noche", command=modonoche)
Add these two line to your code:
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)