【问题标题】:<<TclError: image "pyimage*" doesn't exist>> in python<<TclError: image "pyimage*" doesn't exist>> in python
【发布时间】:2020-03-13 09:29:00
【问题描述】:

我是前几天学习python的学生,现在对'tkinter'很感兴趣。

我不知道下面的代码和错误消息哪里出了问题。

请帮帮我..TT

# -*- coding: utf-8 -*-
import tkinter as tk

banana=r'banana.gif'
bodercolor=[('aliceblue','#F0F8FF'),('blue','#000FF'),
             ('beige','#F5F5DC'),('cornsilk','#FFF8DC'),
              ('red','#ff0000'),('lightgreen','#90EE90')]

class BgChange:
    def __init__(self, label, color):   
        self.label = label
        self.color = color

    def __call__(self, event=None):
        self.label.configure(bg=self.color)

class MyWindow(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.master.title('select bordercolor')
        f_button=tk.Frame(self)
        f_button.pack(side=tk.LEFT, padx=5, pady=1)
        self.banana=tk.PhotoImage(file = banana)     
        label=tk.Label(self, image=self.banana,relief=tk.RAISED, bd=6)
        label.pack(side=tk.RIGHT,padx=7)

        for name, code in bodercolor:
            b=tk.Button(f_button, text=name,
                        bg=code, command=BgChange()) 
            b.pack(fill=tk.X)

if __name__== '__main__':
    MyWindow(tk.Tk()).mainloop()

错误信息

runfile('C:/Users/User/Desktop/python_ex/report/문제2.py', wdir='C:/Users/User/Desktop/python_ex/report')
Traceback (most recent call last):

  File "<ipython-input-14-8a31f2388234>", line 1, in <module>
    runfile('C:/Users/User/Desktop/python_ex/report/문제2.py', wdir='C:/Users/User/Desktop/python_ex/report')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/User/Desktop/python_ex/report/문제2.py", line 35, in <module>
    win = MyWindow(root)

  File "C:/Users/User/Desktop/python_ex/report/문제2.py", line 24, in __init__
    label=tk.Label(self, image=self.banana,relief=tk.RAISED, bd=6)

  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 2766, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)

  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))

TclError: image "pyimage10" doesn't exist

【问题讨论】:

  • ``` 文件“”,第 35 行,在 MyWindow(tk.Tk()).mainloop() 文件“”,第 24 行,在 init 标签=tk.Label(self, image=self.banana,relief=tk.RAISED, bd=6) 文件“C:\ProgramData\Anaconda3 \lib\tkinter_init_.py",第 2766 行,在 init Widget.__init__(self, master, 'label', cnf, kw) File "C:\ProgramData \Anaconda3\lib\tkinter_init_.py",第 2299 行,在 init (widgetName, self._w) + extra + self._options(cnf)) TclError: image “pyimage2”不存在```

标签: tkinter


【解决方案1】:

欢迎来到 Stackoverflow。 你应该提供一个Minimal Reproducible Example

根据你的错误:

TclError:图像“pyimage10”不存在

我提供了在我的情况下的完整\绝对路径

self.banana='C:\\Users\\Python\\banana.gif'

我还保留了对图像的引用:

self.master.banana= PhotoImage(file = self.banana) 

完整代码如下:

# -*- coding: utf-8 -*-
from tkinter import Tk, Label, Frame, PhotoImage

class MyWindow():
    def __init__(self, master=None):
        self.banana='C:\\Users\\Python\\banana.gif'
        self.master = master
        self.master.title('select bordercolor')

        self.f_button = Frame(self.master)
        self.f_button.pack(side= "left", padx=5, pady=1)
        self.master.banana= PhotoImage(file = self.banana) 

        self.label= Label(self.master, image=self.master.banana, relief="raised", bd=6)
        self.label.pack(side="right", padx=7)

if __name__== '__main__':
    root = Tk()
    MyWindow(root)
    root.mainloop()

您的代码中还有其他错误,但这是另一个值得考虑的问题。请参阅按钮中的 tkinter 命令选项!

【讨论】:

  • 非常感谢您的建议!我试试看,谢谢!
  • 如果能解决您的问题,请将此问题设置为已回答
【解决方案2】:

我在使用 tkinter 时发现了同样的错误,但有趣的是,我注意到一种模式,有时它会工作,有时它会显示此错误!

解决这个问题的简单方法是先运行这段代码:

将 tkinter 导入为 tk

root = tk.TK()

root.mainloop()

之后将显示多个 Tkinter 窗口,只需关闭所有窗口,然后运行您的代码 从头开始,即再次导入库,然后运行代码。

我希望这能解决问题。

【讨论】:

  • 请使用正确的代码格式,不要全部大写。
猜你喜欢
  • 2018-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 2021-01-15
相关资源
最近更新 更多