【发布时间】:2021-08-11 14:38:54
【问题描述】:
我尝试在 python 中打开图像,代码如下:
from tkinter import *
from PIL import ImageTk, Image
main = Tk()
main.geometry("500x500")
main.title("Geometry Helper")
main.config(bg="Light Green")
img = PhotoImage(Image.open("C:\\Users\\Lenovo User\\Downloads\\Geommy Dash.PNG"))
Yoeshabite = Label(main, image=img)
label1 = Label(main, text="Geometry Dash Helper", font=" Verdana 12", bg="Light Green").place(x=155,y=130)
label2 = Label(main, text="An app designed by MellowMoex", bg="Light Green").place(x=155,y=150)
eeeeeEe = Button(main, text="Search", bg="Dark Green", command="Searchj").place(x=155,y=170)
eeeeEEe = Button(main, text="Level requests", bg="Green").place(x=206,y=170)
main.mainloop()
但它会标记此错误:
Traceback (most recent call last):
File "c:\Users\Lenovo User\OneDrive - Colchester Royal Grammar School\Documents\Geometyr dash.py", line 8, in <module>
Yoeshabite = Label(main, image=img)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 3148, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 2572, in __init__
self.tk.call(
TypeError: __str__ returned non-string (type PngImageFile)
【问题讨论】:
-
将
img = PhotoImage(...)更改为img = ImageTk.PhotoImage(...) -
没用对不起
-
你给你报错了吗?你也使用
utton(..., command="Searchj")。这是不正确的,因为command参数只能是可调用函数/lambda -
TheLizzard 是对的。这应该工作
img=ImageTk.PhotoImage(Image.open('name.png'))
标签: python image tkinter label