pythonimagetkinterphotoimage

I am using PhotoImage in python to make a image, but it cannot find a image


I am creating a python board game, but i cant even put my board on my GUI! I am using:

from tkinter import *
root = Tk()
panel = Label(root, image = "board.gif")
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

But it returns:

Traceback (most recent call last):
  File "/Users/GAMEKNIGHT7/Desktop/genius hour/chineseCheckersAI(genius hour).py", line 3, in <module>
    label = Label(image="board.gif")
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2760, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2293, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "board.gif" doesn't exist

What happened? How can i do this?


Solution

  • You must use the PhotoImage class:

    from tkinter import *
    root = Tk()
    img = PhotoImage(file="board.gif")
    panel = Label(root, image=img)
    panel.pack(side="bottom", fill="both", expand="yes")
    root.mainloop()