Last Updated : 12 Jul, 2025
Tkinter, the standard Python library for creating graphical user interfaces (GUIs), provides a powerful widget called the Canvas that allows us to draw and manipulate shapes. The Canvas widget in Tkinter is an excellent tool for building 2D graphics. Our task is to create various shapes such as ovals, rectangles, arcs, and polygons using the Canvas class.
Canvas Methods for ShapesHere are some essential methods provided by the Canvas class to create various shapes:
This Python code uses Tkinter to create a simple GUI that displays various shapes on a canvas. The Shape class defines methods to draw ovals, rectangles, arcs, and polygons using the Canvas widget. These shapes are customized with different colors and line widths. The canvas is packed into the main window, and the program runs in the Tkinter event loop, allowing the shapes to be displayed interactively.
Python
from tkinter import *
from tkinter.ttk import *
class Shape:
def __init__(self, master=None):
self.master = master
self.create()
def create(self):
self.canvas = Canvas(self.master)
self.canvas.create_oval(10, 10, 80, 80,
outline="black", fill="white",
width=2)
self.canvas.create_oval(110, 10, 210, 80,
outline="red", fill="green",
width=2)
self.canvas.create_rectangle(230, 10, 290, 60,
outline="black", fill="blue",
width=2)
self.canvas.create_arc(30, 200, 90, 100, start=0,
extent=210, outline="green",
fill="red", width=2)
points = [150, 100, 200, 120, 240, 180,
210, 200, 150, 150, 100, 200]
self.canvas.create_polygon(points, outline="blue",
fill="orange", width=2)
self.canvas.pack(fill=BOTH, expand=1)
if __name__ == "__main__":
master = Tk()
shape = Shape(master)
master.title("Shapes")
master.geometry("330x220+300+300")
mainloop()
Output:
Explanation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4