Python Tkinter Canvas: Complete Guide
The Canvas widget in Tkinter is a powerful tool for adding graphics, shapes, and drawings to Python GUI applications. Use it to build charts, graphs, games, and custom drawing tools.
Complete Python Course:-
Complete Advance AI topics:-
Syntax
canvas = Canvas(parent, options)
Where parent is the parent window or frame and options are configuration attributes.
Common Canvas Options
bdÔÇö border width (default 2)bgÔÇö background colorwidth/heightÔÇö canvas dimensionscursorÔÇö cursor type (arrow, circle, dot)reliefÔÇö border style (SUNKEN, RAISED, GROOVE)scrollregionÔÇö scrollable area (x1, y1, x2, y2)xscrollcommand/yscrollcommandÔÇö attach scrollbars
Example 1: Basic Canvas
from tkinter import *
top = Tk()
top.geometry("200x200")
c = Canvas(top, bg="pink", height=200, width=200)
c.pack()
top.mainloop()
Example 2: Draw an Arc
from tkinter import *
top = Tk()
top.geometry("200x200")
c = Canvas(top, bg="pink", height=200, width=200)
c.create_arc((5, 10, 150, 200), start=0, extent=150, fill="white")
c.pack()
top.mainloop()
More Drawing Methods
create_line(x1, y1, x2, y2)ÔÇö straight linecreate_rectangle(x1, y1, x2, y2)ÔÇö rectanglecreate_oval(x1, y1, x2, y2)ÔÇö oval/circlecreate_polygon(points)ÔÇö polygoncreate_text(x, y, text="Hello")ÔÇö textcreate_image(x, y, image=img)ÔÇö image
Quick Multi-Shape Demo
from tkinter import *
root = Tk()
c = Canvas(root, width=300, height=200, bg="white")
c.create_rectangle(10, 10, 100, 80, fill="lightblue")
c.create_oval(120, 10, 200, 80, fill="lightgreen")
c.create_line(0, 100, 300, 100, fill="red", width=3)
c.create_text(150, 150, text="UpdateGadh!", font=("Arial", 14))
c.pack()
root.mainloop()
Download New Real Time Projects:- Click here
Conclusion
The Tkinter Canvas widget is incredibly versatile ÔÇö perfect for charts, games, drawing apps, and custom UI elements. Master the basic shapes and event binding, and you can build rich GUI applications. For more tutorials, stay tuned to .
python tkinter canvas documentation
python tkinter canvas create_text
python tkinter canvas methods
python tkinter canvas image
python tkinter canvas example
tkinter canvas create rectangle
tkinter canvas size
python tkinter label