我正在 python 中使用 PhotoImage 制作图像,但找不到图像

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

我正在创建一个 python 棋盘游戏,但我什至无法将我的棋盘放在我的 GUI 上! 我正在使用:

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

但是 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

发生什么事了?我该怎么做?

您必须使用 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()