EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Tkinter button color

Secondary Sidebar
Tkinter Tutorial
  • Tkinter
    • Python Tkinter
    • Tkinter Widgets
    • Tkinter background image
    • Tkinter button color
    • Tkinter place
    • Python Tkinter Button
    • Python Tkinter Canvas
    • Tkinter Frame
    • Tkinter LabelFrame
    • Python Tkinter Label
    • Tkinter Text
    • Tkinter Scrollbar
    • Tkinter Listbox
    • Tkinter Spinbox
    • Tkinter Checkbutton
    • Tkinter Menu
    • Tkinter Menubutton
    • Tkinter OptionMenu
    • Tkinter Messagebox
    • Tkinter Grid
    • Tkinter Pack
    • Tkinter Scale
    • Tkinter Table
    • Python Tkinter Entry
    • Tkinter after
    • Tkinter Colors
    • Tkinter Font
    • Tkinter PhotoImage
    • Tkinter TreeView
    • Tkinter Notebook
    • Tkinter Combobox
    • Tkinter Bind
    • Tkinter Icon
    • Tkinter Window Size
    • Tkinter Color Chart
    • Tkinter Slider
    • Tkinter Calculator
    • Tkinter geometry
    • Tkinter image
    • Tkinter input box
    • Tkinter mainloop
Home Software Development Software Development Tutorials Tkinter Tutorial Tkinter button color

Tkinter button color

Introduction to Tkinter button color

We can provide any color to the button widget using Tkinter. Button widget has so many properties, out of which ‘bg’ is used to set the background color for the button. We can pass the name of the color or hex value for color using the bg property. We can also change the foreground color by using the ‘fg’ property of the button widget in Tkinter. We can also use the configure method to change the color.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,064 ratings)

Syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

As we know that we can use the ‘bg’ and ‘fg’ property of the Button widget to change the color, we need to pass this variable while using the Button object. In short, while creating the Button object. Let’s see the syntax for better understating see below;

  • By using color string: We can pass color string inside the bg property like below;
  • variable_name = Button(parent, bg=’your_color’)
  • By using hex value: We can also use the HEX value of color to assign it.
  • variable_name = Button(parent, bg=’HEX_VALUE_COLOR’)

For better understating we will see one practice syntax for beginners to understand it better;

button1 = Button(parent, bg=’RED’)

button2 = Button(parent, bg=’#fff’)

How to color button in Tkinter?

As of now, we know that we can use the bf and fg property to change the color of the button widget. We also have one more option to change the color of the button by using the configure method. To implement this method, we have the ‘command’ property available in the button widget; by the use of it, we can call the configure method to change color. To use the Button widget, we have to have a Tkinter module in our program in the first place because it is the object of the Tkinter module, and we know that Tkinter is used to create the GUI in python. Now we will see examples to change the bg and fg color of the button and discussed them in detail. See below;

1. To change the bg and fg color by using the Button widget

Button widget as two properties to change the background and foreground color of the button by using bg and fg keywords, respectively.

Example:

from tkinter import *
parent = Tk()
parent.geometry('500x500')
button1 = Button(parent, text = 'click me!', fg='red', bg='yellow' )
button1.pack()
parent.mainloop()

In the above example, we are changing the background and foreground color of the button widget. For this first, we have imported the Tkinter module into our program. After this, we create a Tkinter object responsible for creating the GUI window for us; we can assign the window size by using the geometry function; where else, it will take the default size. Immediately after this, we are creating a button object by initializing the bg and fg color property. After that, we call the pack method to assign our button object a fixed position in the window, and in the end, we are calling the mainloop() method to initialize the window.

2. By using the configure method, to change color

We can use the configure method to change the color of the button widget, but to use this, we need to create one function and call it from the command property of the button widget.

Example:

from tkinter import *
def demoColorChange(): button1.configure(bg="red", fg="yellow")
parent = Tk()
parent.geometry('500x500')
button1 = Button(parent, text = 'click me!', command= demoColorChange )
button1.pack()
parent.mainloop()

As we can see, we are creating one method here called ‘demoColorChange’ and calling this method from the command property of the button widget so that it will change the background and foreground color of the button. This is another way of calling it.

Constructor

As we know, color is the button widget’s property, so this itself does not contain the constructor of it. But the Button widget has its constructor with it with a different number of parameters inside it. We can discuss it in details see below;

Example:

variable_name = Button(parent, bg='your_color', fg = 'color', text= 'your_text', commnd = 'your_function'..)
  • Parent: This parameter is responsible for creating the window object. So we need to create a Tkinter object and pass it inside this.
  • bg: This parameter is responsible for assigning a background color to the widget.
  • fg: This parameter is responsible for assigning foreground color to the widget.
  • text: This parameter is responsible for providing text on the button.
  • command: By using this parameter, we can call our function and assign them color as well.
  • height: By using this parameter, we can assign height.

Methods

Here also it does not have any methods with it because it’s a Button widget property, but we can use the configure method to change the color as well see below;

Example:

Configure: By using this, we can call our custom functions which are responsible for performing a certain task for us;

def function_name(): widget.configure(bg="red", fg="yellow")

We can call this method on any widget, but here we are using a button to change the color; this can be called on any widget.

Examples of Tkinter button color

Given below are the examples of Tkinter button color:

Example #1

In this example, we are changing the background color of the button using the bg property.

Code:

from tkinter import *
parent = Tk()
#size of window
parent.geometry('500x500')
#titlt to window
parent.title('Button color demo !!')
#creating button and assiging color
button = Button(parent, text = 'Click me !!', bg='red', height = 5, width = 10)
button.pack()
#initilizing window
parent.mainloop()

Output:

Tkinter button color output 1

Example #2

In this example, we are changing the foreground color of the button using the fg property.

Code:

from tkinter import *
parent = Tk()
#size of window
parent.geometry('500x500')
#titlt to window
parent.title('Button color demo !!')
#creating button and assiging color
button = Button(parent, text = 'Click me !!', bg='yellow',  fg = 'blue', height = 5, width = 10)
button.pack()
#initilizing window
parent.mainloop()

Output:

Tkinter button color output 2

Conclusion

Bu the use of this color property, we can assign different colors to the button widget. For this, we are using the bg and fg property of the button. By assigning different colors to the button, we can make our GUI more interactive to the user also; we can also perform some events, like on click of a button; we can again change the button color.

Recommended Articles

This is a guide to the Tkinter button color. Here we discuss How to color buttons in Tkinter along with the Examples, constructors, and Methods. You may also have a look at the following articles to learn more –

  1. Tkinter OptionMenu
  2. Tkinter TreeView
  3. Tkinter Colors
  4. Tkinter Scrollbar
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More