Python3 tkinter 设置图片大小 [英] Python3 tkinter set image size

查看:1130
本文介绍了Python3 tkinter 设置图片大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have looked everywhere to find a way to set the size of an image. The image is set to a url. I have found other questions on the site but none of them have worked.

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()

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

That is the code that creates the image, how would I set the size of the image

推荐答案

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

As mentioned by several others, you should use PIL to resize your image before attaching it to a tkinter label:

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()

这篇关于Python3 tkinter 设置图片大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆