Como mudar a cor do botão de Tkinter
- Definir a cor do botão do Tkinter
-
Alterar a cor do Tkinter
Button
com o métodoconfigure
-
Altera o
Button
Cor com obg
/fg
Atributos
O widget Tkinter Button
tem atributos bg
e fg
para definir as cores de fundo e de primeiro plano. Poderíamos atribuir cores a bg
e fg
quando inicializamos o objeto Button
e alterar a cor do Tkinter Button
com o método configure
ou atribuir novos valores às teclas bg
e fg
.
Definir a cor do botão do Tkinter
import tkinter as tk
class Test:
def __init__(self):
self.root = tk.Tk()
self.root.geometry("250x100")
self.buttonA = tk.Button(self.root, text="Color", bg="blue", fg="red")
self.buttonB = tk.Button(
self.root, text="Click to change color", bg="gray", fg="purple"
)
self.buttonA.pack(side=tk.LEFT)
self.buttonB.pack(side=tk.RIGHT)
self.root.mainloop()
app = Test()
Você também poderia substituir bg
por backgron
, fg
por foreground
para definir o Tkinter Botão
de fundo e cor de primeiro plano.
import tkinter as tk
class Test:
def __init__(self):
self.root = tk.Tk()
self.root.geometry("250x100")
self.buttonA = tk.Button(
self.root, text="Color", background="blue", foreground="red"
)
self.buttonB = tk.Button(
self.root,
text="Click to change color",
background="gray",
foreground="purple",
)
self.buttonA.pack(side=tk.LEFT)
self.buttonB.pack(side=tk.RIGHT)
self.root.mainloop()
app = Test()
Alterar a cor do Tkinter Button
com o método configure
Depois que o widget Tkinter Button
for criado, poderemos mudar sua cor utilizando o método configure
.
import tkinter as tk
class Test:
def __init__(self):
self.root = tk.Tk()
self.root.geometry("250x100")
self.buttonA = tk.Button(self.root, text="Color", bg="blue", fg="red")
self.buttonB = tk.Button(
self.root, text="Click to change color", command=self.changeColor
)
self.buttonA.pack(side=tk.LEFT)
self.buttonB.pack(side=tk.RIGHT)
self.root.mainloop()
def changeColor(self):
self.buttonA.configure(bg="yellow")
app = Test()
self.buttonA.configure(bg="yellow")
Ele configura o background
ou igualmente o bg
para ser amarelo
.
Altera o Button
Cor com o bg
/fg
Atributos
bg
e fg
são chaves
do dicionário de objetos Tkinter Button
, portanto, poderíamos mudar a cor do Tkinter Button
atribuindo novos valores a essas chaves
.
import tkinter as tk
class Test:
def __init__(self):
self.root = tk.Tk()
self.root.geometry("250x100")
self.buttonA = tk.Button(self.root, text="Color", bg="blue", fg="red")
self.buttonB = tk.Button(
self.root, text="Click to change color", command=self.changeColor
)
self.buttonA.pack(side=tk.LEFT)
self.buttonB.pack(side=tk.RIGHT)
self.root.mainloop()
def changeColor(self):
self.buttonA["bg"] = "gray"
self.buttonA["fg"] = "cyan"
app = Test()
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook