-2

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?

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
F. Zeng
  • 85
  • 1
  • 2
  • 11
  • Possible duplicate of [I don't show the image in Tkinter](https://stackoverflow.com/questions/38370560/i-dont-show-the-image-in-tkinter) – snipsnipsnip Nov 05 '18 at 04:48
  • 2
    @snipsnipsnip That is not a good duplicate. The OPs problem is more related to missing PhotoImage all-together rather than simple not saving a reference to the image. – Mike - SMT Nov 05 '18 at 12:51
  • You're right. I retracted the flag. – snipsnipsnip Nov 06 '18 at 08:15

1 Answers1

2

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()
Faquarl
  • 335
  • 3
  • 11