Change colors in program using Tkinter

Asked

Viewed 2,280 times

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.

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

follow:

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

1 answer

1


All right, buddy? You didn’t succeed because you referred to "night" on def Padrao , where the name of your window was set to "start", not to "night"; Adjusting this, I then added the light color; It is important to note that you can switch to the colors you wish, not necessarily to those I defined (Light Grey and Black); Here is the code with the requested modifications:

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 I’ve helped, good studies!

  • excellent help, thank you very much Vitor! attended what I needed

  • @Victors' I’m glad I helped you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.