EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Tkinter Tutorial Tkinter Colors
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

Tkinter Colors

By Abhilasha ChouguleAbhilasha Chougule

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.

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Examples of Tkinter Colors

Lets us discuss the examples of Tkinter Colors.

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,697 ratings)

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
Popular Course in this category
Tkinter Course (1 Course, 1 Project)
  1 Online Courses |  1 Hands-on Projects |  6+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
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