我已经有一组代码类似于下面的格式,这似乎有效。但不知何故,这个图像并没有出现。它们与代码位于同一文件夹中。 Def small是使图像工作的代码,def stripe是给我一个错误的代码。
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()
这是完整的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" doesn't exist
”或类似的东西时,这意味着tkinter无法决定它是哪个窗口的照片。这是由于多个Tk()
窗户。当你使用多个Tk
时,很少有其他东西会产生问题,这就是为什么Tkinter有另一种类型的窗口Toplevel
并且它像主窗口一样引用主窗口。
让我们来看看你的代码..
在这里,我看到除了那个错误之外的其他几个问题。
Tk()
窗口。我相信你可能有两个以上。mainloop()
一个足以打开尽可能多的Toplevel窗口,但记得在代码的末尾使用至少一个mainloop()
。Photoimage
,该函数将图像存储在局部变量中时,即使它被Label
或Canvas
显示,图像也会被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()