Python 3.x 试图添加一个功能类似于按钮的图像,但出现此错误,image";pyimage2“;不';不存在,弹出窗口

Python 3.x 试图添加一个功能类似于按钮的图像,但出现此错误,image";pyimage2“;不';不存在,弹出窗口,python-3.x,tkinter,tkinter-canvas,ttk,Python 3.x,Tkinter,Tkinter Canvas,Ttk,我已经有了一套与下面的格式类似的代码,而且似乎可以工作。但不知何故,这张照片的图像并没有出现。它们和代码在同一个文件夹中。Def small是使图像工作的代码,而Def stripes是给我一个错误的代码 from tkinter import * import tkinter as tk from tkinter import ttk def small(): s = Tk() s.title('Small Preset Shirt (Not fit to scale)')

我已经有了一套与下面的格式类似的代码,而且似乎可以工作。但不知何故,这张照片的图像并没有出现。它们和代码在同一个文件夹中。Def small是使图像工作的代码,而Def stripes是给我一个错误的代码

from tkinter import *
import tkinter as tk
from tkinter import ttk

def small():
    s = Tk()
    s.title('Small Preset Shirt (Not fit to scale)')
    canvas = Canvas(s, width = 800, height = 100)
    canvas.pack()
    b1=ttk.Button(s,text='Click to Start', command = questions)
    b1.pack()
    photo = PhotoImage(file = 'small.png')
    b1.config(image=photo,compound=RIGHT)
    s.mainloop()

def stripes():
    stripes = Tk()
    stripes.title('Black Shirt with Stripes')
    canvas = Canvas(stripes, width = 800, height = 100)
    canvas.pack()
    b2=ttk.Button(stripes,text='Click to See Final Price', command = final)
    b2.pack()
    photo = PhotoImage(file = 'stripes.png')
    b2.config(image=photo,compound=RIGHT)
    stripes.mainloop()

以下是完整的回溯:

Exception in Tkinter callback
Traceback (most recent call last):
File              "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter        /__init__.py", line 1705, in __call__
return self.func(*args)
File "/Users/Business/Documents/Python/small.py", line 159, in  stripes
b2.config(image=photo,compound=RIGHT)
File  "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter.   /__init__.py", line 1485, in configure
return self._configure('configure', cnf, kw)
File   "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter.   /__init__.py", line 1476, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "pyimage2" doesn't exist

当您收到错误消息“
\u tkinter.TclError:image“pyimage2”不存在
”或类似信息时,表示tkinter无法确定它是哪个窗口的照片。这是由于存在多个
Tk()
windows。当您使用多个
Tk
时,几乎没有其他东西会造成问题,这就是为什么Tkinter有另一种类型的窗口
Toplevel
,它像子窗口一样引用主窗口

让我们进入您的代码。

在这里,除了这个错误,我看到了一些其他的问题

  • 就像我说的,不要超过一个
    Tk()
    窗口。我相信你可能有两个以上

  • 如果您有一个主窗口,并且决定使用Toplevel再打开几个,那么请不要使用另一个
    mainloop()
    一个窗口足以打开尽可能多的Toplevel窗口,但请记住在代码末尾至少使用一个
    mainloop()

  • 有时,在将图像存储在局部变量中的函数中定义
    Photoimage
    时,python会清除该图像,即使该图像是通过
    标签
    画布
    显示的。因此,在这种情况下,请始终创建一个引用

  • 由于您的代码不可运行,所以我添加了必要的东西来运行和测试它

    from tkinter import *
    from tkinter import ttk
    
    Main_window = Tk()  # Make only one Tk main window 
    Main_window.geometry('300x150')
    Main_window.title("Get Shirts (Buy 1 get 1 Free)")
    
    def small():
        s = Toplevel()   # For secondary window use Toplevel 
        s.title('Small Preset Shirt (Not fit to scale)')
        canvas = Canvas(s, width = 800, height = 100)
        canvas.pack()
        b1=ttk.Button(s,text='Click to Start', command = None)
        b1.pack()
        photo = PhotoImage(file = 'logo.png')
        b1.img_ref = photo      # Create a reference 
        b1.config(image=photo,compound=RIGHT)
        # s.mainloop()      # Don't use mainloop more than once
    
    
    def stripes():
        stripes = Toplevel()  # For secondary window use Toplevel 
        stripes.title('Black Shirt with Stripes')
        canvas = Canvas(stripes, width = 800, height = 100)
        canvas.pack()
        b2=ttk.Button(stripes,text='Click to See Final Price', command = None)
        b2.pack()
        photo = PhotoImage(file = 'logo.png')
        b2.img_ref = photo      # Sometimes images in functions becomes garbage value.
        b2.config(image=photo,compound=RIGHT)
        # stripes.mainloop()      # Using two of these will do nothnig.
    
    
    Category_Lb = Label(Main_window, text='Category', font=('',25))
    Category_Lb.pack()
    
    Cate_1 = ttk.Button(Main_window, text='Small Preset Shirt', command=small)
    Cate_1.pack()
    
    Cate_2 = ttk.Button(Main_window, text='Black Shirt with Stripes', command=stripes)
    Cate_2.pack()
    
    
    Main_window.mainloop()
    

    你是如何调用这两个函数的?您是否使用线程同时执行这两个函数?