Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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 使用Tkinter时,错误:TclError:image";pyimage8“;不';不存在_Python_Tkinter_Spyder - Fatal编程技术网

Python 使用Tkinter时,错误:TclError:image";pyimage8“;不';不存在

Python 使用Tkinter时,错误:TclError:image";pyimage8“;不';不存在,python,tkinter,spyder,Python,Tkinter,Spyder,我一直收到错误,Tcl错误:图像“pyimage8”不存在。 这很奇怪,因为每次我运行它时,数字都会增加 我使用spyder运行python,不知道这是否会影响任何东西。 这是我的密码 #import the modules we need, for creating a GUI import Tkinter as tk from PIL import ImageTk, Image homescreenImage = PhotoImage(file="Homescreen.gif") #c

我一直收到错误,Tcl错误:图像“pyimage8”不存在。 这很奇怪,因为每次我运行它时,数字都会增加

我使用spyder运行python,不知道这是否会影响任何东西。 这是我的密码

#import the modules we need, for creating a GUI
import Tkinter as tk
from PIL import ImageTk, Image

homescreenImage = PhotoImage(file="Homescreen.gif") 

#create a GUI window.
root = Tk()
#set the title.
root.title("Welcome to the Pit!")
#set the size.
root.geometry("1100x700")

homescreenFrame = tk.Frame(root, width=1100, height = 700,)
homescreenFrame.pack()
homescreenLabel = tk.Label(homescreenFrame, image=homescreenImage)
homescreenLabel.pack()


#start the GUI
root.mainloop()

如果
将Tkinter导入为tk
,则在调用tk时应使用别名tk,例如
root=tk.tk()
。否则Python将找不到Tk

您不需要为此导入
PIL

在创建Tk之前,不能创建照片图像

试试这个:

import Tkinter as tk

root = tk.Tk()
root.title("Welcome to the Pit!")
root.geometry("1100x700")

homescreenImage = tk.PhotoImage(file="Homescreen.gif") 
homescreenFrame = tk.Frame(root, width=1100, height = 700,)
homescreenFrame.pack()
homescreenLabel = tk.Label(homescreenFrame, image=homescreenImage)
homescreenLabel.pack()

root.mainloop()

友好一点,把整个错误信息也贴在你的问题上

以下可能是错误:

1) 给出文件名的完整路径 例如:“/home/user/Homescreen.gif”

2) 如果您使用的是windows,但上述操作不起作用: 使用“\\C:\\home\\Homescreen.gif”(这是因为windows会混淆)

3) 如果这也不起作用,请确保python的目录 程序与图像的程序相同

4) 此外,仅在创建根之后创建photoimage 窗户

5) 由于某些原因,在调试器中运行时,如果以前有 执行时抛出了错误,我得到了“pyimage不存在”错误。 但是,如果我重新启动调试器(或者以前没有执行脚本 已抛出错误),则程序运行正常

6) 另外,不要导入PIL,这不是必需的

尝试以上所有方法,如果不起作用,请告诉我。
希望这有帮助。

我发现我的脚本会运行一次,然后在以后的运行中给我一个错误。如果我重新启动控制台,它将再次运行。我在脚本开头使用以下代码解决了问题:

import sys
if "Tkinter" not in sys.modules:
    from Tkinter import *

现在它每次都能工作。

图像是否在运行此脚本的同一文件夹中?此代码会引发各种错误。请发一封信。