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

时间:2018-11-05 03:52:09

标签: python image tkinter photoimage

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

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

但是它返回:

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

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

1 个答案:

答案 0 :(得分:2)

您必须使用PhotoImage类:

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