【问题标题】:struggling to open image in python努力在python中打开图像
【发布时间】: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


【解决方案1】:

将图像放在文件所在的同一目录中,然后使用 img = PhotoImage(file='Geommy Dash.PNG') 如果它不起作用,请尝试使用 GIF 格式的图像 instaead

【讨论】:

    【解决方案2】:

    根据this的帖子你需要使用place方法。

    • 并且,正如评论所言,它应该是 img = ImageTk.PhotoImage(...) 而不是 img = PhotoImage(...)
    img = ImageTk.PhotoImage(Image.open('chelsea.png'))
    Yoeshabite = Label(main, image=img)
    
    # https://stackoverflow.com/questions/10158552/how-to-use-an-image-for-the-background-in-tkinter
    Yoeshabite.place(x=0, y=0, relwidth=1, relheight=1)
    

    我将您的图片替换为来自here'chelsea.png' 图片(将图片与 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"))
    img = ImageTk.PhotoImage(Image.open('chelsea.png'))
    Yoeshabite = Label(main, image=img)
    
    # https://stackoverflow.com/questions/10158552/how-to-use-an-image-for-the-background-in-tkinter
    Yoeshabite.place(x=0, y=0, relwidth=1, relheight=1)
    Yoeshabite.image = img  # if you are doing this inside a function, make sure you keep a reference.
    
    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()
    

    这是输出:

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 2021-12-19
      • 2016-01-25
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2020-07-13
      • 2022-10-02
      • 1970-01-01
      相关资源
      最近更新 更多