Tkinter <&书信电报;错误:image";pyimage*”;不';t存在>&燃气轮机;用python

Tkinter <&书信电报;错误:image";pyimage*”;不';t存在>&燃气轮机;用python,tkinter,Tkinter,几天前我是一名学习python的学生,现在我对tkinter很感兴趣 我不知道下面的代码和错误消息哪里出错了 请帮帮我 # -*- coding: utf-8 -*- import tkinter as tk banana=r'banana.gif' bodercolor=[('aliceblue','#F0F8FF'),('blue','#000FF'), ('beige','#F5F5DC'),('cornsilk','#FFF8DC'),

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

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

请帮帮我

# -*- 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
runfile('C:/Users/User/Desktop/python\u ex/report/문제2.py',wdir='C:/Users/User/Desktop/python\u ex/report')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
runfile('C:/Users/User/Desktop/python\u ex/report/문제2.py',wdir='C:/Users/User/Desktop/python\u ex/report')
文件“C:\ProgramData\Anaconda3\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,第827行,在运行文件中
execfile(文件名、命名空间)
文件“C:\ProgramData\Anaconda3\lib\site packages\spyder\u kernels\customize\spydercustomize.py”,第110行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“C:/Users/User/Desktop/python\u ex/report/문제2.py“,第35行,in
win=MyWindow(根目录)
文件“C:/Users/User/Desktop/python\u ex/report/문제2.py“,第24行,初始__
label=tk.label(self,image=self.banana,relief=tk.凸起,bd=6)
文件“C:\ProgramData\Anaconda3\lib\tkinter\\uuuuu init\uuuuu.py”,第2766行,在\uuu init中__
小部件。_u初始化(自、主、标签、cnf、kw)
文件“C:\ProgramData\Anaconda3\lib\tkinter\\uuuuu init\uuuuu.py”,第2299行,在\uuuu init中__
(widgetName,self._w)+额外+自选项(cnf))
Tcl错误:映像“pyimage10”不存在
欢迎来到Stackoverflow。 您应该提供一个最小可重复性示例

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
根据您的错误:

Tcl错误:映像“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命令选项

我在与tkinter合作时发现了相同的错误,但有趣的是,我注意到了一种模式,比如有时它会工作,有时它会显示此错误

解决此问题的简单方法是首先运行以下代码:

将tkinter作为tk导入

root=tk.tk()

root.mainloop()

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


我希望这能解决这个问题。

“文件”,第35行,在MyWindow(tk.tk()).mainloop()文件中,第24行,在init label=tk.label(self,image=self.banana,relief=tk.RAISED,bd=6)文件“C:\ProgramData\Anaconda3\lib\tkinter\u init\uuuuuuu.py”,第2766行,在init小部件中“C:\ProgramData\Anaconda3\lib\tkinter_init_uuu.py”,第2299行,在init(widgetName,self._w)+extra+self._options(cnf))TclError:image“pyimage2“不存在”``真的很感谢你的建议!我试试看,谢谢!请将此问题设置为已回答,如果可以解决问题,请使用正确的代码格式,不要键入所有大写字母。