简体   繁体   中英

Cannot resize Image with tkinter

I am trying to create a tkinter project in python 2.7, where the user can resize the window, and everything inside the window will scale with it. This means the canvas, the shapes in the canvas and most importantly the PhotoImages will scale with the window. My problem, is for the life of me I cannot get my images to resize properly. I know that subsample and zoom exist for this purpose, but first of all

plantImage = PhotoImage(file="images/Arable_Cell.gif")
plantImage.subsample(2, 2)
canvas.create_image(0, 0, anchor=NW, image=plantImage)

Makes no noticeable change in a 50x50 pixel image and same for zoom(2, 2). It is important to note I know PIL exists but for the purposes of this project I cannot download any extra libraries. So what am I doing wrong?

According to the docs ,

subsample(self, x, y='')

Return a new PhotoImage based on the same image as this widget but use only every Xth or Yth pixel.

Ie subsample doesn't modify the image, it creates a new one, so try this instead:

originalPlantImage = PhotoImage(file="images/Arable_Cell.gif")
displayPlantImage = originalPlantImage.subsample(2, 2)
canvas.create_image(0, 0, anchor=NW, image=displayPlantImage)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM