I want an event to happen when I click on a particular image on tkinter.

Asked 1 years ago, Updated 1 years ago, 431 views

How do I make an event happen when I click on an image?
(Viewing images on tkinter.)

a=tkinter.PhotoImage(file="○○.png")
  canvas.create_image(x,y,image=a)

I would like to click on this image to generate an event.

python python3 tkinter

2023-02-20 07:24

1 Answers

The image format must be set to GGIF, PPM, or PGM 」 when using the Tkinter Photo Imaging feature.Otherwise, you can use PILLOW to convert files.

Using tkinter as a PhotoImage

#photoimage
import tkinter as tk

root=tk.Tk()

canvas=tk.Canvas(root)

a=tk.PhotoImage("maru-maru.gif")#can use gif,ppm and pgm
canvas.create_image(x,y,a)

root.mainloop()

Using tkinter and PILLOW as PhotoImage

 pip install pillow
#Using pillow
from PIL.ImageTk import PhotoImage

import tkinter as tk

...
# Write your code here
a=PhotoImage ("maru-maru-file.jpg")
...


2023-02-20 08:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.