在python Tkinter中调整大小和图像时出错
我正在学习Tkinter,我想调整图像的大小,这是我的代码:在python Tkinter中调整大小和图像时出错,python,python-3.x,tkinter,Python,Python 3.x,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.jp
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
回溯(最近一次呼叫最后一次):
文件“C:/Users/dvill/PycharmProjects/Programacion/Mi Trabajo/iconos_e_imagenes2.py”,第12行,在
我的标签=标签(图像=img)
文件“C:\Users\dvill\AppData\Local\Programs\Python\Python38-32\lib\tkinter\\uuuuuu init\uuuuu.py”,第3143行,在uu init中__
小部件。_u初始化(自、主、标签、cnf、kw)
文件“C:\Users\dvill\AppData\Local\Programs\Python\Python38-32\lib\tkinter\\ uuuuuu init\uuuuu.py”,第2567行,在uuu init中__
自我呼叫(
_tkinter.TclError:映像“”不存在
任何帮助都将不胜感激!您的标签上有
img
而不是new\u 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()