Open In App

Change color of button in Python – Tkinter

Last Updated : 29 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to write a Python script to change the color of the button in Tkinter. It can be done with two methods:

  • Using bg properties.
  • Using activebackground properties.

Prerequisite: Creating a button in tkinter, Python GUI – Tkinter

Change the color of the button in Python – Tkinter

Example 1: using bg properties.

If you want to change the background color dynamically after the button has been created, you need to use a method call to change the bg (background color) property.

Python
# Import tkinter module 
from tkinter import *

# Function to change button color
def change_color():
    button.config(bg='red')  # Change the color to red

# Create a tkinter window 
master = Tk() 

# Open window having dimension 200x100 
master.geometry('200x100') 
	
# Create a Button with an initial color
button = Button(master, 
                text='Submit', 
                bg='blue', 
                 # Call change_color when the button is clicked
                command=change_color) 
button.pack() 

master.mainloop()

Output:

Example 2: Using activebackground properties.

These properties will change the button background color while clicking the button.

Python
# import tkinter module 
from tkinter import *   

# create a tkinter window
master = Tk()  

# Open window having dimension 200x100
master.geometry('200x100')  
  
# Create a Button
button = Button(master, 
                text = 'Submit', 
                bg='white', 
                activebackground='blue').pack()  
  
master.mainloop()

Output:


Next Article
Article Tags :
Practice Tags :

Similar Reads