How to Change Tkinter Button Color

Jinku Hu Feb 02, 2024
  1. Set Tkinter Button Color
  2. Change Tkinter Button Color With configure Method
  3. Change Tkinter Button Color With bg/fg Attributes
How to Change Tkinter Button Color

Tkinter Button widget has attributes bg and fg to set the background and foreground colors. We could assign colors to bg and fg when we initialize the Button object, and change Tkinter Button color with configure method or assign new values to bg and fg keys.

Set Tkinter Button Color

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

Tkinter Button Set Background and Foreground Color

You could also replace bg with background, fg with foreground to set the Tkinter Button background and foreground color.

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

Change Tkinter Button Color With configure Method

After the Tkinter Button widget is created, we could change its color by using the configure method.

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

Tkinter set Button color

self.buttonA.configure(bg="yellow")

It configures the background or equally bg to be yellow.

Change Tkinter Button Color With bg/fg Attributes

bg and fg are keys of Tkinter Button widget object dictionary, therefore, we could change the Tkinter Button color by assigning new values of these keys.

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

Tkinter set Button color with bg and fg attributes

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

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

Related Article - Tkinter Button