首先,我对 Python 还很陌生,我承认我的代码是杂乱无章的。不过,关于实际问题,基本上我使用 pygame 将图像加载到变量中。我在 tkinter 的顶层使用该图像。当我尝试在该顶层的标签中使用图像时,它说它不存在。我正在使用我在某些领域创建的模块,但我认为它没有任何关系,所以我不包括它。
import tkinter as tk
from myfunctions import *
import pygame
master = tk.Tk()
master.resizable(0,0)
info = "Guess a number between \n 1 and 99 inclusive!"
f=tk.Frame(master, width=500, height=450)
f.pack_propagate(0)
f.pack()
l=tk.Label(f, text=info, font=("Arial", 30))
l.pack()
heat=tk.Label(f, font=("Arial", 24), fg="red", pady=30)
heat.pack()
f2= tk.Frame(f)
f2.pack()
spinbox = tk.Spinbox(f2, from_= 1, to = 99)
spinbox.pack()
bframe=tk.Frame(f, pady =30)
bframe.pack()
def itsago():
valid = validcheck(spinbox.get())
if valid == 0:
heat.config(text="Not a valid input")
heat.pack()
else:
hcheck = hiddencheck(valid)
heat.config(text=hcheck)
heat.pack()
if hcheck == "You won!\n Re-Open the program to play again.":
pwin= tk.Toplevel()
pwin.resizable(0, 0)
sloth = pygame.image.load('paradesloth.png')
lparade = tk.Label(pwin, anchor = "nw")
lparade.config(image = sloth)
lparade.image = sloth
lparade.pack()
pwin.mainloop()
b = tk.Button(bframe, command = itsago, text="Check Number", font=("Arial", 20), bg="white")
b.pack()
master.mainloop()
任何帮助将不胜感激 :)
*编辑:忘记添加最重要的事情之一,错误:
File "C:\Python33\lib\tkinter\__init__.py", line 1254, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "<Surface(800x599x24 SW)>" doesn't exist