Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

I.e. 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...