尝试添加像按钮一样功能的图像,但弹出此错误图像 "pyimage2" 不存在
Trying to add an image that functions like a button, but this error, image "pyimage2" doesn't exist, pops up
我已经有一组代码,其格式与下面的代码类似,而且似乎可以工作。但不知何故,这张图片并没有弹出。它们与代码位于同一文件夹中。 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
当您收到错误消息“_tkinter.TclError: image "pyimage2" doesn't exist
”或类似的消息时,这意味着 tkinter 无法确定它是哪个 window 的照片。这是由于不止一个 Tk()
windows。当您使用多个 Tk
时,很少有其他事情会产生问题,这就是为什么 Tkinter 有另一种类型的 window Toplevel
并且它指的是主要的 window 之类的一个 child window。
让我们开始你的代码..
除了那个错误,我发现其他问题很少。
就像我说的不超过一个 Tk()
window。我相信你应该不止两个。
如果你有一个主 window 并决定用 Toplevel 再打开几个那么请不要使用另一个 mainloop()
一个足以打开尽可能多的 Toplevel windows 但请记住在代码末尾至少使用一个 mainloop()
。
有时,当您在将图像存储在局部变量中的函数中定义 Photoimage
时,图像会被 python 清除,即使它是由 Label
或 Canvas
。所以在这种情况下总是创建一个参考。
因为你的代码不 运行nable 所以我添加了必要的东西到 运行 并测试它。
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()
我已经有一组代码,其格式与下面的代码类似,而且似乎可以工作。但不知何故,这张图片并没有弹出。它们与代码位于同一文件夹中。 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
当您收到错误消息“_tkinter.TclError: image "pyimage2" doesn't exist
”或类似的消息时,这意味着 tkinter 无法确定它是哪个 window 的照片。这是由于不止一个 Tk()
windows。当您使用多个 Tk
时,很少有其他事情会产生问题,这就是为什么 Tkinter 有另一种类型的 window Toplevel
并且它指的是主要的 window 之类的一个 child window。
让我们开始你的代码..
除了那个错误,我发现其他问题很少。
就像我说的不超过一个
Tk()
window。我相信你应该不止两个。如果你有一个主 window 并决定用 Toplevel 再打开几个那么请不要使用另一个
mainloop()
一个足以打开尽可能多的 Toplevel windows 但请记住在代码末尾至少使用一个mainloop()
。有时,当您在将图像存储在局部变量中的函数中定义
Photoimage
时,图像会被 python 清除,即使它是由Label
或Canvas
。所以在这种情况下总是创建一个参考。
因为你的代码不 运行nable 所以我添加了必要的东西到 运行 并测试它。
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()