0% found this document useful (0 votes)
15 views

The Widget Classes - Canvas

widget class in tkinter

Uploaded by

Vinayak.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

The Widget Classes - Canvas

widget class in tkinter

Uploaded by

Vinayak.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

UNIVERSITY INSTITUTE OF COMPUTING

MASTER OF COMPUTER APPLICATIONS

PYTHON PROGRAMMING

DISCOVER . LEARN . EMPOWER


1
Contents
• Processing events-
• The widget classes- canvas-
• The geometry managers-display images-

2
Canvas Widget
The Canvas widget lets us display various graphics on the application. It can be used to
draw simple shapes to complicated graphs. We can also display various kinds of custom
widgets according to our needs.
Syntax:
C = Canvas(root, height, width, bd, bg, ..)
Optional parameters:
root = root window.
height = height of the canvas widget.
width = width of the canvas widget.

3
Canvas Widget
bg = background colour for canvas.
bd = border of the canvas window.
scrollregion (w, n, e, s)tuple defined as a region for scrolling left, top, bottom and right.
highlightcolor colour shown in the focus highlight.
cursor It can be defined as a cursor for the canvas which can be a circle, a do, an arrow
etc.
confine decides if canvas can be accessed outside the scroll region.
relief type of the border which can be SUNKEN, RAISED, GROOVE and RIDGE.

4
Canvas Widget
Some common drawing methods:
Creating an Oval
oval = C.create_oval(x0, y0, x1, y1, options)
Creating an arc
arc = C.create_arc(20, 50, 190, 240, start=0, extent=110, fill="red")
Creating a Line
line = C.create_line(x0, y0, x1, y1, ..., xn, yn, options)
Creating a polygon
oval = C.create_polygon(x0, y0, x1, y1, ...xn, yn, options)

5
Canvas Widget
Example 1: Simple Shapes Drawing
from tkinter import *
root = Tk()
C = Canvas(root, bg="yellow", height=250, width=300)
line = C.create_line(108, 120, 320, 40, fill="green")
arc = C.create_arc(180, 150, 80, 210, start=0, extent=220, fill="red")
oval = C.create_oval(80, 30, 140, 150, fill="blue")
C.pack()
mainloop()

6
Canvas Widget
Output:

7
The geometry managers-display images
Tkinter is a Python module that is used to develop GUI (Graphical User Interface)
applications. It comes along with Python, so you do not have to install it using the pip
command.
Tkinter provides many methods, one of them is the geometry() method. This method is
used to set the dimensions of the Tkinter window and is used to set the position of the
main window on the user’s desktop.
Tkinter window without using geometry method.
# importing only those functions which are needed
from tkinter import Tk, mainloop, TOP
from tkinter.ttk import Button

8
The geometry managers-display images
# creating tkinter window
root = Tk()
# Create Button and add some text
button = Button(root, text = 'Geeks')
# pady is used for giving some padding in y direction
button.pack(side = TOP, pady = 5)
# Execute Tkinter
root.mainloop()

9
The geometry managers-display images
Output:
As soon as you run the application, you’ll see the position of the Tkinter window is at the
northwest position of the screen and the size of the window is also small as shown in the
output below.

10
The geometry managers-display images

11
References
1. https://www.cs.mcgill.ca/~hv/classes/MS/TkinterPres/#WhatIsTk
2. https://www.tutorialspoint.com/how-to-install-tkinter-in-python
3. https://www.python.org/about/gettingstarted/
4. https://www.javatpoint.com/python-tutorial
5. https://www.learnpython.org/

12
THANK YOU

13

You might also like