Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我正在使用python中的PhotoImage制作图像,但它找不到图像_Python_Image_Tkinter_Photoimage - Fatal编程技术网

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

我正在使用python中的PhotoImage制作图像,但它找不到图像,python,image,tkinter,photoimage,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 "/Use

我正在创建一个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
回溯(最近一次呼叫最后一次):
文件“/Users/GAMEKNIGHT7/Desktop/genius hour/chineseCheckersAI(genius hour.py)”,第3行,在
标签=标签(image=“board.gif”)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/_init__.py”,第2760行,在_init中__
小部件。_u初始化(自、主、标签、cnf、kw)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/_init__.py”,第2293行,在_init中__
(widgetName,self._w)+额外+自选项(cnf))
_tkinter.TclError:图像“board.gif”不存在

发生了什么事?如何执行此操作?

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

@snipsnipsnip的可能副本不是一个好的副本。OPs的问题更多的是与丢失照片图像有关,而不是简单的不保存对图像的引用。你是对的。我收回了国旗。