Change colors in program using tkinter

1

Hello, I need to create a function or maybe two to change the background color of the main window without interfering with subsequent events.

I wanted to add in the menu> Environment: Light color pattern; Night: dark color, and I want to bind when I click on the checkbuttons to change the colors.

follows:

from tkinter import *
import tkinter.filedialog

class homepage:
  def __init__(self):
    inicio = Tk()

    inicio.title("Pena Software")
    inicio.geometry("720x580+100+50")
    inicio['bg'] = '#F2F2F2'
    inicio.iconbitmap(r'pena.ico')

    def Open(): tkFileDialog.askopenfilename()
    def Quit(): inicio.destroy()

    menubar = Menu(tearoff=False)
    inicio.config(menu=menubar)



    MENUarquivo = Menu(tearoff=False)
    menubar.add_cascade(label="Arquivo", menu=MENUarquivo)
    MENUarquivo.add_command(label="Criar Sorteio",)
    MENUarquivo.add_command(label="Abrir...",)
    MENUarquivo.add_command(label="Editar Sorteios",)
    MENUarquivo.add_separator()
    MENUarquivo.add_command(label="Sair", command=Quit)


    MENUferramentas = Menu(tearoff=False)
    menulang = Menu(tearoff=False)
    menubar.add_cascade(label="Ferramentas", menu=MENUferramentas)

    menulang.add_command(label="Português",)
    menulang.add_command(label="English",)
    MENUferramentas.add_cascade(label="Linguagens", menu=menulang)

 def Padrao():
    noite["bg"] = "Black"


    menuamb = Menu(tearoff=False)
    menuamb.add_checkbutton(label="Padrão",)
    menuamb.add_checkbutton(label="Noite...", command=Padrao)
    MENUferramentas.add_cascade(label="Ambiente", menu=menuamb)


    MENUajuda = Menu(menubar, tearoff=False)
    MENUajuda.add_command(label="Sobre", command=sobre)
    MENUajuda.add_command(label="Como Usar?",)
    menubar.add_cascade(label="Ajuda", menu=MENUajuda)

    # Por Fim, a janela:
    inicio.mainloop()

def sobre():# uma pequena função "sobre"

    root = Tk()
    root.geometry("240x110+75+75")
    root.title("Sobre")


    texto=("Pena_Software.v0.1_Estável")
    textONlabel = Label(root, text=texto)
    textONlabel.pack()


    lb2 = Label(root, text="Licença Livre")
    lb2.pack()

homepage()
    
asked by anonymous 19.01.2018 / 19:57

1 answer

1

All right, my friend !? You did not succeed because you referred to "night" in def Padrao , and your window name was set to "start," not "night"; Adjusting this, after, I added the light color; It is important to note that you can change to the colors you want, not necessarily those I defined (Light Gray and Black); Here is the code with the requested changes:

from tkinter import *
import tkinter.filedialog

    class homepage:
      def __init__(self):
        inicio = Tk()

        inicio.title("Pena Software")
        inicio.geometry("720x580+100+50")
        inicio['bg'] = '#F2F2F2'
        #inicio.iconbitmap(r'pena.ico')

        def Open(): tkFileDialog.askopenfilename()
        def Quit(): inicio.destroy()

        menubar = Menu(tearoff=False)
        inicio.config(menu=menubar)

        MENUarquivo = Menu(tearoff=False)
        menubar.add_cascade(label="Arquivo", menu=MENUarquivo)
        MENUarquivo.add_command(label="Criar Sorteio",)
        MENUarquivo.add_command(label="Abrir...",)
        MENUarquivo.add_command(label="Editar Sorteios",)
        MENUarquivo.add_separator()
        MENUarquivo.add_command(label="Sair", command=Quit)


        MENUferramentas = Menu(tearoff=False)
        menulang = Menu(tearoff=False)
        menubar.add_cascade(label="Ferramentas", menu=MENUferramentas)

        menulang.add_command(label="Português",)
        menulang.add_command(label="English",)
        MENUferramentas.add_cascade(label="Linguagens", menu=menulang)

        def escuro():
            inicio.configure(background="black")

        def claro():
            inicio.configure(background="light gray")


        menuamb = Menu(tearoff=False)
        menuamb.add_checkbutton(label="Padrão", command=claro)
        menuamb.add_checkbutton(label="Noite...", command=escuro)
        MENUferramentas.add_cascade(label="Ambiente", menu=menuamb)


        MENUajuda = Menu(menubar, tearoff=False)
        MENUajuda.add_command(label="Sobre", command=sobre)
        MENUajuda.add_command(label="Como Usar?",)
        menubar.add_cascade(label="Ajuda", menu=MENUajuda)

        inicio.mainloop()

    def sobre():# uma pequena função "sobre"

        root = Tk()
        root.geometry("240x110+75+75")
        root.title("Sobre")


        texto=("Pena_Software.v0.1_Estável")
        textONlabel = Label(root, text=texto)
        textONlabel.pack()


        lb2 = Label(root, text="Licença Livre")
        lb2.pack()

    homepage()

I hope to have helped, good studies!

    
20.01.2018 / 19:57