EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Tkinter Tutorial Tkinter Colors
 

Tkinter Colors

Abhilasha Chougule
Article byAbhilasha Chougule
EDUCBA
Reviewed byRavi Rathore

Updated April 14, 2023

Tkinter Colors

 

 

Definition of Tkinter Colors

Tkinter color is defined as a property to use any colors for designing the front end of the web development this can be defined in two ways either by using the locally defined color names which are already stored in the database or using the combination of red, blue and green color’s hexadecimal values to get other colors in Tkinter. In general, the Tkinter is designed for developing a web application which would be incomplete without colors, therefore, colors are very important and are hence defined in any way as the user or the developer ease and there is an option for setting background and foreground colors in Tkinter for making the web app more attractive to the user.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Functions of Tkinter Colors

In this article, we are discussing the color property of Tkinter which is a GUI library provided by Python programming language for designing web applications. In Tkinter, the color property is mainly used for setting the colors in the application to make it look attractive to the users. The Tkinter color is defined or declared in two ways the colors are specified using two ways in Tkinter and they are first is we can define or name the color by using the locally defined colors which are used from the given database of its library such as “red”, “blue’, “green”, “black”, “white”, etc and the other way to declare the colors are using the combination of “red”, “blue” and “green” color’s hexadecimal value to obtain colors such as for white #fff, for black #000000, etc which can be in string specification for the proportion of color in hexadecimal value which can be 4, 8 or 12 bit per color. In Python, the Tkinter provides the color chart also for the developers that can be used for developing an attractive mobile or web apps or game apps.

Now let us see how to use Tkinter colors along with examples such as how to set the background color, foreground color, etc. There are many different options provides by the class color in Tkinter such as highlightcolor which is used for setting foreground color when the widget has the focus of the highlight region, highlightbackground for setting the background color, selectbackground which is used for setting the background color for the widget items which is selected, selectforeground for setting foreground color for the selected widget items, activebackground for setting to the active widget for background color, activeforeground for setting foreground color having active widgets, etc.

To use Tkinter first we have to import Tkinter and now let us see the example for setting the background color of the window using bg property and using color names and hexadecimal value.

Examples of Tkinter Colors

Lets us discuss the examples of Tkinter Colors.

Example 1

So to set background color for window or buttons or textbox or textarea, etc there are different ways in Python Tkinter such as we can use the configuration method (configure()), using bg or background property, using color names, using color names with hexadecimal value. Now in the below

import Tkinter as tk
import tkMessageBox
def func():
    tkMessageBox.showinfo( "Hello Tkinter", "Hello Educba")    
root =tk.Tk()
frame = tk.Frame(root)
frame.pack()
bt = tk.Button(frame, text ="Button1", bg = "red", command = func)
bt.pack(side=tk.LEFT)
bt1= tk.Button(frame, text ="Button2", background = "#856ff8", command = func)
bt1.pack(side=tk.TOP)
bt2= tk.Button(frame, text ="Button3", activebackground = "yellow", command = func)
bt2.pack(side=tk.RIGHT)
root.mainloop()

Output:

tkinter colour 1-1

hello tkinter

In the above program, we can see we have first imported the Tkinter package so that we can work for designing the GUI part of any web development applications. In the above code, we are demonstrating how to set the colors of the buttons which we have created and each button color definition is done different ways as we can see in the first button we are using the “bg” property for setting the background color of button1 with RGB color name as “red”, then for button2 we have used “background” property to set the color using the hexadecimal value of light blue color with value as “#856ff8 and then in button3 we have defined “activebackground” property for setting the button background color when we click over the button then it shows “yellow” color. Similarly, we can also set colors for foreground properties also by using the same above code along with any different ways of defining colors.

In Python, Tkinter is used for GUI applications where each has windows and we can set the color to windows along with resolution size we can also change colors of text in the textbox or textarea, etc. So we can also get a chart of different variety of colors and we can also display all these colors too.

Let us see a simple  program of how to set background window color proper defined resolution and the code is as below:

Example 2

import Tkinter as tk
print("Hello Tkinter", "Hello Educba")
wd = tk.Tk()
wd.geometry('500x300')
wd['background'] = '#58F'
wd.mainloop()

tkinter colour 1-2

In the above code we can see first we have imported Tkinter and then we are creating a parent window or root window using the Tkinter alias name so that we can place widgets in this parent window and we have placed a window of having the size described using geometry function with proper pixel specification and then we have set the background color to the hexadecimal value as  ‘#58F’ and then we should always close this parent window by declaring mainloop(). Thus in this way, we can even set the background color of the window also. In general, Tkinter provides these color options to almost all widgets in the Tkinter windows.

Conclusion

In this article, we conclude that the Python provides Tkinter for designing GUI for web applications and in this article, we have discussed one property or option which is most commonly used with all widgets defined inside the parent window and that is the color. In this article, we saw that there are different options such as background, foreground, activebackground, etc for specifying the color to the widgets and we also how we can define the color for the buttons and windows in this article along with examples and we can use such concepts in defining colors to any widgets in the Tkinter of Python programming language.

Recommended Articles

This is a guide to Tkinter Colors. Here we discuss the Definition and working of Tkinter Colors along with different examples and code implementation. You may also have a look at the following articles to learn more –

  1. Tkinter Menu
  2. Tkinter Text
  3. Tkinter Scrollbar
  4. Tkinter Spinbox

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

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

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

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

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW