Python3 tkinter 设置图像大小

新手上路,请多包涵

我到处寻找一种方法来设置图像的大小。图像设置为 url。我在网站上发现了其他问题,但没有一个有效。

 import urllib.request, base64

u = urllib.request.urlopen(currentWeatherIconURL)
raw_data = u.read()
u.close()

b64_data = base64.encodestring(raw_data)
image = PhotoImage(data=b64_data)

label = Label(image=image, bg="White")
label.pack()

那是创建图像的代码,我将如何设置图像的大小

原文由 SeanOMik 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 853
1 个回答

正如其他几个人所提到的,在将图像附加到 tkinter 标签之前,您应该使用 PIL 调整图像大小:

 from tkinter import Tk, Label
from PIL import Image, ImageTk

root = Tk()

img = ImageTk.PhotoImage(Image.open('img-path.png').resize(pixels_x, pixels_y)) # the one-liner I used in my app
label = Label(root, image=img, ...)
label.image = img # this feels redundant but the image didn't show up without it in my app
label.pack()

root.mainloop()

原文由 Nelson 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题