Header Ads

Header ADS

Adding Image on Tkinter Canvas



Image on Tkinter Canvas.

Welcome back to another blog on tkinter , In This blog you will learn how to add image on tkinter canvas widget using create_image function and lots more about it .

syntax:

canvas.create_image(x,y,image = image_file,anchor )

  • The x and y denotes at which  distance from x and y axis the image should be placed.
  • image = image_file , takes the image object as argument which is created using photoimage method .
  • anchor means which part of the image you want to display , anchor option have multiple values .

possible values of anchor option are.




So lets code .
       			 
# Tkinter - Dynamic Coding
# Image on canvas widget ..

# Import Library
from tkinter import *

root = Tk()
root.title("Dynamic Coding")
root.geometry('300x250')

can = Canvas(root,height=200,width=250)
can.place(x=0,y=0)

# Adding image ...

# creating a photoimage object ....
img = PhotoImage(file='C:\\Users\\alex\\Desktop\\Python-logo.png')
# it is complusary to use double backward slashes \\ for window users .

can.create_image(10,10,image=img,anchor=NW)
can.image = img  # optional ..

# can.image = img is used when you are continuosly changing the image of canvas .

root.mainloop()
 

Output.

Instead of NW if we pass CENTER the output is like.
 


you can check out our Tkinter tutorial series .
and leave a comment whether it is useful for you or not .

.



Recommended Posts:





Thank you ..
If have Any doubts and Errors , comment section is open for you and we will try to solve your doubts as soon as possible.

No comments

Powered by Blogger.