简体   繁体   English

TKinter python 3.x调整图像大小

[英]TKinter python 3.x resizing an image

I would like to resize an image using TKinter. 我想使用TKinter调整图像大小。 Please note that I will not be using PIL for this. 请注意,我不会为此使用PIL。

How can I currently have this image, which works fine. 我目前如何获得这张图片,效果很好。

logo = PhotoImage(file="logo_dribbble-01_1x.PNG")
label = Label(f1,image=logo, borderwidth=0, highlightthickness=0)
label.pack()

logo_dribbble-01_1x.PNG

I would like to resize this image so that the logo looks smaller. 我想调整此图像的大小,以使徽标看起来更小。

I tried doing this, which was suggested here 我尝试这样做,这在这里建议

smallLogo = PhotoImage(file="logo_dribbble-01_1x.PNG")
smallLogo = smallLogo.subsample(2, 2)
smallLabel = Label(f1,image=smallLogo, borderwidth=0, highlightthickness=0)
smallLabel.pack()

But this creates an empty label without displaying the image. 但这会创建一个空标签而不显示图像。

I tried to resize the image using Photoshop and use that image and then use that .png image to display the smaller image like so: 我尝试使用Photoshop调整图像的大小并使用该图像,然后使用该.png图像来显示较小的图像,如下所示:

logo = PhotoImage(file="logo_dribbble-01_1xsmall.PNG")
smallLabel = Label(f1,image=smallLogo, borderwidth=0, highlightthickness=0)
smallLabel.pack()

logo_dribbble-01_1xsmall.PNG

But, I get this error when I try to run the code _tkinter.TclError: encountered an unsupported criticial chunk type "mkBF" 但是,当我尝试运行代码_tkinter.TclError: encountered an unsupported criticial chunk type "mkBF"时,出现此错误_tkinter.TclError: encountered an unsupported criticial chunk type "mkBF"

How can I resolve this issue? 我该如何解决这个问题?

The following code worked for me: 以下代码为我工作:

from tkinter import *

f1 = Tk()
smallLogo = PhotoImage(file="image.PNG")
smallLogo = smallLogo.subsample(2, 2)
smallLabel = Label(f1,image=smallLogo, borderwidth=0, highlightthickness=0)
smallLabel.pack()
f1.mainloop()

Note I'm on tk-8.6 注意我在tk-8.6上

I had to keep a reference of the image that was being used by the label like so: 我必须保留标签使用的图像的引用,如下所示:

logo = PhotoImage(file="image.png")
logo = logo.subsample(2, 2)
label = Label(root,image=logo, borderwidth=0, highlightthickness=0)
label.image = logo
label.pack()
smallLogo = PhotoImage(file="logo_dribbble-01_1x.PNG")
smallLogo_one = smallLogo.subsample(2, 2)
smallLabel = Label(f1,image=smallLogo_one, borderwidth=0, highlightthickness=0)
smallLabel.pack()

I think this will solve the problem for you.Your variable for PhotoImage is the same as variable for the Subsample to trim the image for you. 我认为这将为您解决问题。您的PhotoImage变量与“子样本”变量可以为您修剪图像。 I change the variable for subsample to smallLogo_one the parsed it to the image attribute in the Lable. 我将子样本的变量更改为smallLogo_one,将其解析为标签中的image属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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