python - 在 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()
推荐阅读
- php - 对于巨大的表格数据,使用 div 标签是否比使用 table tr 和 td 标签更快?
- javascript - 跨字符串数组映射以生成“选择”选项(反应/节点)
- reactjs - 仅在流星订阅完成后如何在反应组件构造函数中设置初始状态值
- java - 如何在此代码中处理 StringIndexOutOfBoundException
- google-apps-script - 从 Google 表格中读取电子邮件列表,但仅从每封假定的电子邮件中接收字符串“范围”
- regex - grep:尝试查找回文时反向引用无效
- javascript - 在使用其方法之前定义谷歌地图
- ruby-on-rails - 将变量传递给 Rails SLIM 视图
- haskell - 尝试制作一个基本的haskell程序
- php - 使用 arduino 将数据发送到服务器