本文介绍了试图添加一个功能类似于按钮的图像,但是这个错误,图像“pyimage2"不存在,弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一组格式与下面类似的代码,而且这似乎有效.但不知何故,这个图像没有弹出.它们与代码位于同一文件夹中.Def small 是使图像正常工作的代码,而 def stripes 是给我一个错误的代码.

I already have a set of code that is similarly formatted as the one below, and that seems to work. But somehow, the image for this one isn't popping up. And they're in the same folder as the code. Def small is the code that has the image working, and def stripes is the one that is giving me an error.

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

这是完整的回溯:


Here is the full Traceback:

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

推荐答案

当您收到错误消息_tkinter.TclError: image "pyimage2" does not exist" 或类似内容时意味着 tkinter 无法确定它是哪个窗口的照片.这是由于多个 Tk() 窗口造成的.当您使用多个 Tk 时,几乎没有其他事情会产生问题,这就是为什么 Tkinter 有另一种类型的窗口 Toplevel 并且它像子窗口一样引用主窗口窗户.

When you get an error message "_tkinter.TclError: image "pyimage2" doesn't exist" or something like that then it means tkinter can't decide which window's photo it is. This is due to of more than one Tk() windows. There are few other things that create problems when you use more than one Tk, that is why Tkinter have another type of window Toplevel and it refers to the main window like a child window.

让我们来看看你的代码..

在这里,除了那个错误之外,我几乎没有发现其他问题.

Here I see few other problems other than just that error.

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

  1. Like I said not more than one Tk() window. I believe you probably have more than two.

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

If you have a main window and decide to open few more with Toplevel then please don't use another mainloop() one is sufficient to open as many Toplevel windows but remember to use at least one mainloop() at the end of your code.

有时当您在将图像存储在局部变量中的函数中定义 Photoimage 时,即使 Label画布.所以在这种情况下总是创建一个引用.

Sometimes when you define a Photoimage in a function which stored an image in a local variable the image is cleared by python even if it’s being displayed by the Label or Canvas. So always create a reference in that case.

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

As your code is not runnable so I added necessary things to run and test it.

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

这篇关于试图添加一个功能类似于按钮的图像,但是这个错误,图像“pyimage2"不存在,弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 23:41