【问题标题】:When using Tkinter, error: TclError: image "pyimage8" doesn't exist使用 Tkinter 时,错误:TclError: image "pyimage8" doesn't exist
【发布时间】:2018-10-05 07:52:04
【问题描述】:

我不断收到错误消息,TclError: image "pyimage8" doesn't exist。 很奇怪,每次运行的时候数字都会增加?

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

这是我的代码:

#import tkinter
import Tkinter as tk
  
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()

【问题讨论】:

标签: python tkinter spyder


【解决方案1】:

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

您不需要为此导入PIL

您不能在创建 Tk 之前创建 Photoimage。

试试这个:

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

请善待并在您的问题中粘贴整个错误消息。

【讨论】:

    【解决方案2】:

    以下可能是错误:

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

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

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

    4) 此外,仅在创建根目录后创建照片图像 窗口。

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

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

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

    【讨论】:

      【解决方案3】:

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

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

      现在每次都有效。

      【讨论】:

        【解决方案4】:

        就我而言,这是因为我忘记保留对图像的引用。创建标签后尝试添加此行:

        homescreenLabel.image=homescreenImage.
        

        【讨论】:

          【解决方案5】:

          我认为这可能是由于:

          tkinter 只支持.png 格式的图片 但是,还有其他方法可以添加.gif`` instead of PhotoImage```

          【讨论】:

            猜你喜欢
            • 2020-03-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-04-09
            • 2020-11-04
            • 2012-06-25
            • 2020-11-04
            相关资源
            最近更新 更多