简体   繁体   English

在 python Tkinter 中调整大小和图像时出错

[英]Error when resizing and image in python Tkinter

I'm learning Tkinter and I want to resize an image, this is my code:我正在学习 Tkinter 并且我想调整图像大小,这是我的代码:

from tkinter import *
from PIL import ImageTk, Image

root = Tk()
root.title("Iconos e Imagenes")
root.geometry("500x500+60+70")
root.iconbitmap("logo.ico")

img = Image.open("image.jpg")
img = img.resize((200,248), Image.ANTIALIAS)
new_img = ImageTk.PhotoImage(img)
my_Label = Label(image= img)
my_Label.pack()

button_close = Button(root, text="Close Program", command=root.quit)
button_close.pack()
root.mainloop()

And i'm getting this error:我收到了这个错误:

Traceback (most recent call last):
  File "C:/Users/dvill/PycharmProjects/Programacion/Mi Trabajo/iconos_e_imagenes2.py", line 12, in <module>
    my_Label = Label(image= img)
  File "C:\Users\dvill\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3143, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\dvill\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__
    self.tk.call(
_tkinter.TclError: image "<PIL.Image.Image image mode=RGB size=200x248 at 0x386F1C0>" doesn't exist

Any help would be appreciated!任何帮助,将不胜感激!

you had img instead of new_img by your lable你的标签上有img而不是new_img

try this尝试这个

from tkinter import *
from PIL import ImageTk, Image

root = Tk()
root.title("Iconos e Imagenes")
root.geometry("500x500+60+70")
root.iconbitmap("logo.ico")

img = Image.open("image.jpg")
resize_img = img.resize((200, 248), Image.ANTIALIAS)
new_img = ImageTk.PhotoImage(resize_img )
my_Label = Label(root, image=new_img)
my_Label.pack()

button_close = Button(root, text="Close Program", command=root.quit)
button_close.pack()
root.mainloop()

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

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