- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Changing the color a Tkinter rectangle on clicking
The Canvas widget in Tkinter is one of the versatile widgets in Tkinter which is used for developing dynamic GUI interface of the application such as shapes, logo, arcs, animating objects, and many more. With the help of the create_rectangle(top, left, bottom, right, **options) constructor, we can create a rectangular shape in our canvas widget. All the Canvas items support multiple features such as shapes property, size, color, outline, etc.
Let us suppose that we want to change the color of the drawn rectangle with the help of a button event. Defining a callback function that extends the property such as fill= color would change the color of the rectangle.
Example
# Import the required libraries from tkinter import * # Create an instance of Tkinter Frame win = Tk() # Set the geometry of Tkinter Frame win.geometry("700x250") # Define a function to change the color of the rectangle def change_color(*args): canvas.itemconfig(shape, fill='blue') # Add a canvas inside the frame canvas = Canvas(win, width=500, height=250) canvas.pack() # Add a rectangle inside the canvas widget shape = canvas.create_rectangle(500, 100, 50, 50, fill='red') # Add a button to change the color of the rectangle button = Button(win, text="Change Color", font=('Helvectica 11'), command = lambda: change_color(canvas)) button.place(relx=.5, rely=.5, anchor=CENTER) win.mainloop()
Output
If we run the above code, it will display a window with a rectangle and a button widget.
On clicking the "Change Color" button, it will change the color of the rectangle to blue.
- Related Articles
- How to change the color of a Tkinter rectangle on clicking?
- Changing the background color of a tkinter window using colorchooser module
- Changing the Mouse Cursor in Tkinter
- How to clear Text widget content while clicking on the Entry widget itself in Tkinter?
- Changing color randomly in JavaScript
- Default window color Tkinter and hex color codes in Tkinter
- Call the same function when clicking a Button and pressing Enter in Tkinter
- Changing the color of an axis in Matplotlib
- Changing the appearance of a Scrollbar in Tkinter (using ttk styles)
- How to change the background color after clicking the button in JavaScript?
- Changing Tkinter Label Text Dynamically using Label.configure()
- Changing the Default Font for all the widgets in Tkinter
- How to colorize the outline of a Tkinter canvas rectangle?
- Changing the color of a single X-axis tick label in Matplotlib
- How to make a Tkinter canvas rectangle transparent?
