Friday, December 27, 2024
Google search engine
HomeLanguagesChange color of button in Python – Tkinter

Change color of button in Python – Tkinter

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

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.

Example 1: using bg properties.

We can change the button background color with bg properties, The default color of the button is grey but here we are going to change.

Python3




# 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='blue').pack()  
  
master.mainloop()


Output:

Example 2: Using activebackground properties.

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

Python3




# 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:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments