G
G
Gennady2018-07-16 12:32:14
Python
Gennady, 2018-07-16 12:32:14

TKinter, how to place a thumbnail of an image?

Good day to all!
I display the image using the Label widget
If I hardcode the dimensions of the image, then the problem is that the image is not visible to the whole, but only a part (the center of the image, if the image is larger than the form or widget on which it is displayed). I have tried all possible options, tried using pack, place and grid.
Tell me, how can I achieve such an effect that the displayed image decreases (or stretches), proportionally, with a change in the size of the parent form? Really the only option is to resize the image before displaying it on the form?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
Xander017, 2018-07-16
@genana40

Try like this:

from tkinter import *
from PIL import Image, ImageTk

class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.columnconfigure(0,weight=1)
        self.rowconfigure(0,weight=1)
        self.original = Image.open('image.jpeg')
        self.image = ImageTk.PhotoImage(self.original)
        self.display = Canvas(self, bd=0, highlightthickness=0)
        self.display.create_image(0, 0, image=self.image, anchor=NW, tags="IMG")
        self.display.grid(row=0, sticky=W+E+N+S)
        self.pack(fill=BOTH, expand=1)
        self.bind("<Configure>", self.resize)

    def resize(self, event):
        size = (event.width, event.height)
        resized = self.original.resize(size,Image.ANTIALIAS)
        self.image = ImageTk.PhotoImage(resized)
        self.display.delete("IMG")
        self.display.create_image(0, 0, image=self.image, anchor=NW, tags="IMG")

root = Tk()
app = App(root)
app.mainloop()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question