Use Variable in Button Attributes

Asked

Viewed 146 times

1

I’m creating a small system in python3 using the Tkinter library, in this system there are many buttons and I would like to standardize the colors using a variable, so I don’t have to keep writing this at all, more or less like this:

padrao = (bg="gray18", fg="green2", activebackground="green2", activeforeground="gray1")

criar_arquivo = Button(container_criar, text="Criar arquivo", command=criar_file, padrao)

but the error and I’ve tried several ways but none worked, and I haven’t found anything on the internet that could help me, is there any way to do this?

1 answer

0


You can add style, see this example:

from tkinter import * 
from tkinter.ttk import * 

root = Tk() 
root.geometry('300x180') 

style = Style() 


# Irá adicionar estilo a todos os botões disponíveis
# mesmo que não estejamos passando estilo
# para cada widget de botão. 
style.configure('TButton', font = 
               ('calibri', 10, 'bold', 'underline'), 
                foreground = 'red') 
# botão 1 
btn1 = Button(root, text = 'Quit !',  
                  style = 'TButton', 
             command = root.destroy) 

btn1.grid(row = 0, column = 3, padx = 100) 

# botão 2 
btn2 = Button(root, text = 'Click me !', command = None) 
btn2.grid(row = 1, column = 3, pady = 10, padx = 100) 

root.mainloop()

Browser other questions tagged

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