0

I am trying to make a slideshow-like program using the tkinter module. Here is what I have done:

from tkinter import *

root = Tk()


photo = PhotoImage(file = "image.jpeg")
w = Label(root, image=photo)
w.pack()

def callback(event):
    print ("clicked at", event.x, event.y)

##canvas= Canvas(root, width=800, height=500)
canvas= Canvas(root)
canvas.bind("<Button-1>", callback)
canvas.pack()

root.mainloop()

what's going on:
I am putting a picture on the canvas.
I am also detecting left clicks and getting the coordinates (which are printed on the shell)

But what really happens:

The picture (which needs to be in the same folder as the script, btw) appears on top, and a little blank space appears underneath, and I can click that (and get coordinates for the click). If I click the picture, nothing happens. When I click the blank space, it only gives the coordinates of the click for the blank space, not counting the picture as part of the area. If I enlarge the window, it just adds inert blank space on the sides, that do not react to being clicked.

My question is, how do I get the picture to be the clickable part (meaning, gets coordinates as well), and remove the blank space

If you can get the picture to enlarge with the window, that's even better.

Python 3.7.3, on MacBook. I only have the standard library.

acw1668
  • 40,144
  • 5
  • 22
  • 34
Aidan Tung
  • 55
  • 6

2 Answers2

0

You don't need the canvas and should bind on the label instead. Note that tkinter.PhotoImage() does not support JPEG image, use PNG or use Pillow module instead.

from tkinter import *
from PIL import ImageTk

root = Tk()

photo = ImageTk.PhotoImage(file="image.jpeg")
w = Label(root, image=photo)
w.pack()

def callback(event):
    print ("clicked at", event.x, event.y)

w.bind("<Button-1>", callback)

root.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • I do not have PIL or pillow (I should probably get it), and `tkinter.PhotoImage()` has worked for me, when using a JPEG image. – Aidan Tung Jan 09 '21 at 04:23
  • I was wondering why I couldn't get jpg to work on my system... – RufusVS Jan 09 '21 at 06:20
  • That means the image is not actually a JPEG image but with a `.jpeg` file extension. Use `file image.jpeg` (does macos has `file` command?) to check its real format. – acw1668 Jan 09 '21 at 08:03
  • If you right click an image file, there is a "get info" option. It opens a little window, that has the file info. there is a line in there that says: Kind: JPEG. If I really am not supposed to be able to use JPEG images, well, I am not sure why it works, but it does. – Aidan Tung Jan 10 '21 at 02:32
0

You are not actually placing the image on the canvas. To place the image on the canvas use .create_image(). To resize the image to the canvas. you can use resize method provided by the PIL library.

from tkinter import *
from PIL import ImageTk, Image

def resize_event(event):
    resized = ImageTk.PhotoImage(img.resize((event.width, event.height), resample = Image.NEAREST))
    canvas.itemconfig(img_item, image=resized)
    canvas.moveto(img_item, 0, 0)
    canvas.image = resized
    
def callback(event):
    print ("clicked at", event.x, event.y)
     

root = Tk()

img = Image.open(r"Imagepath")
photo = ImageTk.PhotoImage(img)
#w = Label(root, image=photo)
#w.pack()

##canvas= Canvas(root, width=800, height=500)
canvas= Canvas(root)
img_item = canvas.create_image(0, 0, image = photo)
canvas.bind("<Button-1>", callback)
canvas.bind('<Configure>', resize_event)
canvas.pack(expand=True, fill='both')

root.mainloop()
JacksonPro
  • 3,135
  • 2
  • 6
  • 29
  • I do not have PIL. Maybe it's supposed to be part of the standard library, but I tried to import and that didn't work. – Aidan Tung Jan 09 '21 at 04:25
  • @AidanTung pip install Pillow – JacksonPro Jan 09 '21 at 04:53
  • pip does not work for me, I have tried it before. In both the shell and the script, it says, "syntax error", and nothing more. – Aidan Tung Jan 10 '21 at 02:35
  • @AidanTung run it from the command prompt or terminal, not from shell or script. – JacksonPro Jan 10 '21 at 02:40
  • Command prompt is the window with the ">>>" in it? I also used this, but cut out the parts that needed PIL, and it works partially. Only about a quarter of it loads on the tkinter window. The window is the right size, and I get coordinates from anywhere on the window, but only part of the picture is there; the rest is white space – Aidan Tung Jan 10 '21 at 02:42
  • @AidanTung are you on windows or mac? command prompt has `path>` – JacksonPro Jan 10 '21 at 02:42
  • I am using Mac. I do not know how to open the command prompt then, only the shell and the empty one that you put the code in – Aidan Tung Jan 10 '21 at 02:45
  • @AidanTung its called terminal in mac. Search for it on the internet – JacksonPro Jan 10 '21 at 03:36
  • Parents banned Terminal, due to some loophole with parental controls, and the ability to play Tetris on it. – Aidan Tung Jan 11 '21 at 04:20
  • @AidanTung You can manually install it, check [this](https://stackoverflow.com/questions/13270877/how-to-manually-install-a-pypi-module-without-pip-easy-install). Or you can search how to install python packages manually on the internet. – JacksonPro Jan 11 '21 at 04:26
  • Thanks, but parents aren't fine with this apparently :( (I am a minor) – Aidan Tung Jan 11 '21 at 04:27