In this article we want to learn How to Create Custom Widget in 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 for creating 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 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 in TKinter 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 17 18 19 20 21 22 |
import tkinter as tk #create tkinter object root = tk.Tk() # Set the title of the window root.title("Codeloop.org - TKinter Canvas") # Set window icon root.iconphoto(True, tk.PhotoImage(file='codeloop.png')) #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() |
Run the complete code and this will be the result.
Creating Custom Widgets in TKinter
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 31 32 33 34 35 36 37 38 39 40 |
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="Click Me") 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() #Set the title of the window root.title("Codeloop.org - TKinter Custom Widget") #Set window icon root.iconphoto(True, tk.PhotoImage(file='codeloop.png')) # 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 Python. 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
Subscribe and Get Free Video Courses & Articles in your Email