1

I have tried to open an image in python here is code:

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()

But It flags up this error:

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)
MattDMo
  • 100,794
  • 21
  • 241
  • 231

2 Answers2

1

According to this post you need to use place method.

  • And, as commented, it should be img = ImageTk.PhotoImage(...) and not 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)

I replaced your image with 'chelsea.png' image from here (placed the image in the same folder as the Python script).


Here is a code sample that supposed to work:

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()

Here is the output:

enter image description here

Rotem
  • 30,366
  • 4
  • 32
  • 65
0

Put the image in the same directory where your file is then use img = PhotoImage(file='Geommy Dash.PNG') If it does not work try using a GIF format of image instaead