0% found this document useful (0 votes)
16 views7 pages

Images

Inheritance
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)
16 views7 pages

Images

Inheritance
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/ 7

Showing Images

An image is just another widget.


photo = PhotoImage(file=‘somefile.gif’)

Note: Tkinter only supports GIF, PGM, PBM, to read JPGs you need to use
the Python Imaging Library

im = PhotoImage(file='cake.gif') # Create the PhotoImage widget


# Add the photo to a label:
w = Label(root, image=im)
# Create a label with image
w.image = im
# Always keep a reference to avoid garbage collection
Guess how you put an image in a
Button?
A Canvas is a container that allows you to show images and draw on the
container. Draw graphs, charts, implement custom widgets (by drawing
on them and then handling mouse-clicks).

A canvas was the widget that Turtle Graphics uses to draw on!

myCanvas = Canvas(root, width=400, height=200)


myCanvas.create_line(0, 0, 200, 100)
myCanvas.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))
myCanvas.create_image(0, 0, anchor=NW, image=myPhotoImage)
Syntax
w = Canvas ( master, option=value,
... ) 1. Bd
Parameters: Border width in
pixels. Default is
master − This represents the parent Bg
2
window. Normal background color.

options − Here is the list of most Confine


If true (the default), the canvas
3
commonly used options for this cannot be scrolled outside of the
scrollregion.
widget. These options can be used as
key-value pairs separated by 4
Cursor
Cursor used in the canvas like
commas. arrow, circle, dot etc.

Example:
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()
Common drawing methods
oval = C.create_oval(x0, y0, x1, y1, options)

arc = C.create_arc(20, 50, 190, 240, start=0, extent=110, fill="red")

line = C.create_line(x0, y0, x1, y1, ..., xn, yn,


options)

oval = C.create_polygon(x0, y0, x1, y1, ...xn, yn, options)


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()

You might also like