EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Tkinter Tutorial Python Tkinter Canvas
Secondary Sidebar
Tkinter Tutorial
  • Tkinter
    • Python Tkinter
    • Tkinter Widgets
    • Tkinter background image
    • Tkinter button color
    • Tkinter place
    • Python Tkinter Button
    • Python Tkinter Canvas
    • Tkinter Frame
    • Tkinter LabelFrame
    • Python Tkinter Label
    • Tkinter Text
    • Tkinter Scrollbar
    • Tkinter Listbox
    • Tkinter Spinbox
    • Tkinter Checkbutton
    • Tkinter Menu
    • Tkinter Menubutton
    • Tkinter OptionMenu
    • Tkinter Messagebox
    • Tkinter Grid
    • Tkinter Pack
    • Tkinter Scale
    • Tkinter Table
    • Python Tkinter Entry
    • Tkinter after
    • Tkinter Colors
    • Tkinter Font
    • Tkinter PhotoImage
    • Tkinter TreeView
    • Tkinter Notebook
    • Tkinter Combobox
    • Tkinter Bind
    • Tkinter Icon
    • Tkinter Window Size
    • Tkinter Color Chart
    • Tkinter Slider
    • Tkinter Calculator
    • Tkinter geometry
    • Tkinter image
    • Tkinter input box
    • Tkinter mainloop

Python Tkinter Canvas

By Priya PedamkarPriya Pedamkar

Python-Tkinter-Canvas

Introduction to Python Tkinter Canvas

Python Tkinter is a standard package in which canvas is a class that helps someone create different shapes with the help of a lot of functions available in it. Here, shapes can be from simple widgets, a text box, to any complex layouts. Tkinter helps in the easy and powerful building of GUI applications. The flexible attributes like coordinates, colors, width, etc., help create any item very easily.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Below is the syntax to create any widget:

w = Canvas ( Master, option=value, ... )
  • Master: “Master” is the parent window, where the widget needs to be imbibed.
  • Option: “Option” is the various parameter related to the widget used.

Examples of Python Tkinter Canvas

Following are the different examples of Python Tkinter Canvas.

Example #1

Code:

import tkinter
top = tkinter.Tk()
Can = tkinter.Canvas(top, bg="red", height=300, width=300)
coord = 100, 100, 300, 200
arc = Can.create_arc(coord, start=0, extent=150, fill="grey")
Can.pack()
top.mainloop()

Output:

Python Tkinter Canvas 1-1

Explanation: As one can see, how the canvas is used first to create a rectangular box. This canvas has a property set like background color, height, and width of the canvas. Now since we have a background set, we put any widget on it like here we have put arc. Arc has been set with certain properties like color and coordinates. Here x1,y1 are (100,100) & x2,y2 are (300,200). Similarly, we can create any widget over the same kind of canvas.

Example #2

Code:

import tkinter
top = tkinter.Tk()
Can = tkinter.Canvas(top, bg="red", height=300, width=300)
oval = Can.create_oval(100, 100, 300, 200, fill="grey")
Can.pack()
top.mainloop()

Output:

oval shape widget

Explanation: The oval shape widget is created over the same kind of canvas. In this way, one can create a widget like a rectangle, arc, line, text, etc. Here Can.pack() is can two attributes in it: fill and expand like below:

Can.pack(fill=tkinter.BOTH,expand = 1)

Tkinter.BOTH refers to the flexibility given to the widget by the manager to expand horizontally and vertically if required. The “expand” can take a value of 0 or 1. If expand = 1, that means it tells the manager to assign extra space to the widget box in order to accommodate the widget in the parent window. Now let’s see how we can embed any image over the canvas.

Example #3

Code:

import tkinter
top = tkinter.Toplevel()
Can = tkinter.Canvas(top, bg="blue", height=500, width=500)
imagename = tkinter.PhotoImage(file = "C:/Users/giphy.gif")
image = Can.create_image(50, 50, image=imagename)
Can.pack()
top.mainloop()

Output:

Python Tkinter Canvas 1-3

Explanation: As one notices, we have the canvas reddy with height, width, and background color. Over which PhotoImage of tkinter package is helping us keep our image as an item over it. We have the image name “giphy.gif” saved at location “C:/Users/”. This location is referred to in the Photo image and then placed over the canvas.

Example #4 – Multiple items in Canvas

Code:

#Tkinter
import tkinter
top = tkinter.Toplevel()
Can = tkinter.Canvas(top, bg="blue", height=500, width=500)
imagename = tkinter.PhotoImage(file = "C:/Users/i505860/OneDrive - SAP SE/Documents/My Folder/Personnel/EDUCBA(Mumbai)/2020_Articles/Feb/giphy.gif")
image = Can.create_image(50, 50, image=imagename)
coord = 10, 50, 400, 200
arc = Can.create_arc(coord, start=0, extent=150, fill="red")
Can.pack(fill=tkinter.BOTH,expand = 1)
top.mainloop()

Output:

Python Tkinter Canvas 1-4

Explanation: When you place two items over the canvas. One gets over top of another item. As shown above, an arc is over the image “giphy.gif”.

Example #5 – Create Polygon

Code:

from tkinter import *
canvas_w = 200
canvas_h =200
python_green = "#476042"
master = Tk()
Can = Canvas(master, 
           width=canvas_w, 
           height=canvas_h)
Can.pack()
points = [0,0,100,100, 0, 100]
Can.create_polygon(points, outline=python_green, 
            fill='red', width=3)
mainloop()

Output:

Polygon Created

Explanation: As one can notice, the polygon is created over the canvas with the color red and created between points: (0,0),(100,100),(0,100). (0,0) always starts from the top left corner. The more points one gives to a polygon, the more customized polygon can be created. Like shown below:

Example #6 – Adding Points

Code:

from tkinter import *
canvas_w = 200
canvas_h =200
python_green = “#476042”
master = Tk()
Can = Canvas(master, 
           width=canvas_w, 
           height=canvas_h)
Can.pack()
points = points = [0, 0, 0, 200, 100, 100, 50, 90]
Can.create_polygon(points, outline=python_green, 
            fill=’green’, width=3)
mainloop()

Output:

Python Tkinter Canvas 1-6

Explanation: We gave the points manually and created the polygon. However, this approach is very tedious and time-consuming.

Example #7 – Deleting the Widget

There are ways of deleting the widget from the canvas.

Code:

import tkinter
top = tkinter.Toplevel()
Can = tkinter.Canvas(top, bg="blue", height=500, width=500)
imagename = tkinter.PhotoImage(file = "C:/Users/giphy.gif")
image = Can.create_image(50, 50, image=imagename)
Can.delete(image)
Can.pack()
top.mainloop()

Output:

Deleting the widget

Explanation: If someone notices, “Can.delete(image)” helped in removing “giphy.gif” from the canvas. If there exists more than one image, and one wants to remove all images over the canvas. One simple code will be:

Can.delete(ALL)

Conclusion

As we saw above, Canvas is a class from package Tkinter that helps in holding items over it. It provides a graphical facility to create objects over the canvas like line, arc, oval, images and other widgets. Even custom widgets can also be explored once one has a good grip over basic widgets. Generalizing the model and presenting it in the form of application is the best one can think of. Hence, knowing Tkinter Canvas and its widget will give a great boost to your python horizon.

Recommended Articles

This is a guide to Python Tkinter Canvas. Here we discuss the basic concept along with different examples and code implementation, respectively. You may also look at the following articles to learn more –

  1. Tkinter Frame
  2. Tkinter LabelFrame
  3. Tkinter Listbox
  4. Python Tkinter
Popular Course in this category
Data Science with Python Training (24 Courses, 14+ Projects)
  24 Online Courses |  14 Hands-on Projects |  110+ Hours |  Verifiable Certificate of Completion
4.8
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more