- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
- Python 3 Useful Resources
- Python 3 - Questions and Answers
- Python 3 - Quick Guide
- Python 3 - Tools/Utilities
- Python 3 - Useful Resources
- Python 3 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python 3 - Tkinter Canvas
The Canvas is a rectangular area intended for drawing pictures or other complex layouts. You can place graphics, text, widgets or frames on a Canvas.
Here is the simple syntax to create this widget −
w = Canvas ( master, option = value, ... )
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
|Sr.No.||Option & Description|
Border width in pixels. Default is 2.
Normal background color.
If true (the default), the canvas cannot be scrolled outside of the scrollregion.
Cursor used in the canvas like arrow, circle, dot etc.
Size of the canvas in the Y dimension.
Color shown in the focus highlight.
Relief specifies the type of the border. Some of the values are SUNKEN, RAISED, GROOVE, and RIDGE.
A tuple (w, n, e, s) that defines over how large an area the canvas can be scrolled, where w is the left side, n the top, e the right side, and s the bottom.
Size of the canvas in the X dimension.
If you set this option to some positive dimension, the canvas can be positioned only on multiples of that distance, and the value will be used for scrolling by scrolling units, such as when the user clicks on the arrows at the ends of a scrollbar.
If the canvas is scrollable, this attribute should be the .set() method of the horizontal scrollbar.
Works like xscrollincrement, but governs vertical movement.
If the canvas is scrollable, this attribute should be the .set() method of the vertical scrollbar.
The Canvas widget can support the following standard items −
arc . Creates an arc item, which can be a chord, a pieslice or a simple arc.
coord = 10, 50, 240, 210 arc = canvas.create_arc(coord, start = 0, extent = 150, fill = "blue")
image . Creates an image item, which can be an instance of either the BitmapImage or the PhotoImage classes.
filename = PhotoImage(file = "sunshine.gif") image = canvas.create_image(50, 50, anchor = NE, image = filename)
line . Creates a line item.
line = canvas.create_line(x0, y0, x1, y1, ..., xn, yn, options)
oval . Creates a circle or an ellipse at the given coordinates. It takes two pairs of coordinates; the top left and bottom right corners of the bounding rectangle for the oval.
oval = canvas.create_oval(x0, y0, x1, y1, options)
polygon . Creates a polygon item that must have at least three vertices.
oval = canvas.create_polygon(x0, y0, x1, y1,...xn, yn, options)
Try the following example yourself −
# !/usr/bin/python3 from tkinter import * from tkinter import messagebox top = Tk() C = Canvas(top, bg = "blue", height = 250, width = 300) coord = 10, 50, 240, 210 arc = C.create_arc(coord, start = 0, extent = 150, fill = "red") line = C.create_line(10,10,200,200,fill = 'white') C.pack() top.mainloop()
When the above code is executed, it produces the following result −