In this article we want to talk about Creating Custom Widgets with Tkinter, when it comes to Python programming language, you have different options for building GUI Applications and TKinter is on of them, Tkinter is powerful GUI library for Python that provides different types of widgets to create desktop applications. also there may be instances where you need custom widget that is not provided by Tkinter. In such cases you can create your own custom widget using Tkinter’s Canvas widget. In this article we will learn how to create custom widgets with Tkinter.
First of all let’s talk about TKinter Canvas, Canvas widget is powerful widget that allows you to draw shapes, images and text on canvas. it provides platform to create custom widgets using the drawing functions provided by Tkinter. to get started, you need to import the Tkinter module and create Canvas object:
1 2 3 4 5 6 7 8 |
import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=500, height=500) canvas.pack() root.mainloop() |
This code creates a window with canvas of width and height of 500 pixels.
Drawing Shapes on the Canvas : Canvas widget provides different methods to draw shapes on the canvas. these are some commonly used methods:
- create_line: Draws line on the canvas.
- create_rectangle: Draws rectangle on the canvas.
- create_oval: Draws an oval on the canvas.
- create_polygon: Draws polygon on the canvas.
This is an example for drawing rectangle on the canvas:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import tkinter as tk #create tkinter object root = tk.Tk() #creat canvas canvas = tk.Canvas(root, width=500, height=500) #add pack layout canvas.pack() #create rectangle canvas canvas.create_rectangle(50, 50, 150, 150, fill="red") #start the loop root.mainloop() |
This code draws red rectangle on the canvas with the top left corner at (50, 50) and bottom right corner at (150, 150).
Run the complete code and this will be the result.

Creating Custom Widgets : Now that we know how to draw shapes on the canvas, we can use these shapes to create custom widgets. this is an example code to create custom button widget:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import tkinter as tk class CustomButton(tk.Canvas): def __init__(self, master=None, **kwargs): super().__init__(master, **kwargs) self.config(width=100, height=50) #create rectangle canvas self.rectangle = self.create_rectangle(0, 0, 100, 50, fill="yellow") #add text to that self.text = self.create_text(50, 25, text="GeeksCoders.com") self.bind("<Button-1>", self.button_click) #bind an event to the button def button_click(self, event): print("Button Clicked!") #create Tk object root = tk.Tk() #create custom button class button = CustomButton(root) #add to the lanyout button.pack() #run the loop root.mainloop() |
This code creates custom button widget by subclassing the Canvas widget. __init__ method creates yellow rectangle and adds text “GeeksCoders.com” to it. It also binds the left mouse button click event to the button_click method.
In this article, we have learned how to create custom widgets with Tkinter. we used Canvas widget to draw shapes and create custom button widget. with this knowledge, you can create your own custom widgets and add them to your desktop applications. Tkinter provides powerful platform to create custom widgets and extend the functionality of your desktop applications.
Run the complete code and this will be the result

Learn More on Python
- Python-Docx: Creating Microsoft Word Documents with Python
- Python and Microsoft Word: A Beginner’s Guide to Automating Documents
- How to Install docx2python: Python Library for Word Documents
- Merge Microsoft Word Documents with Python Docxcompose
- Asynchronous Web Development with Python and aiohttp
- Python Treq: An Introduction to a Powerful HTTP Client Library
- Introduction to Python httplib2 Library
- An Introduction to Python’s urllib Library
- Python httpx: A High-Performance HTTP Client for Python 3