簡體   English   中英

在 python Tkinter 中調整大小和圖像時出錯

[英]Error when resizing and image in python Tkinter

我正在學習 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()

我收到了這個錯誤:

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

任何幫助,將不勝感激!

你的標簽上有img而不是new_img

嘗試這個

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