在我的程序中,我遇到了一个错误,我不知道是什么原因造成的。
一段代码:
sunPic = r'sun.gif'
mercPic = r'merc.gif'
buttonFrame = Frame(root)
buttonFrame.pack(side=LEFT)
textFrame = Frame(root)
textFrame.pack(side=TOP)
def sunInfo():
textFrame.destroy()
sunImage = PhotoImage(file=sunPic)
img1 = Label(textFrame, image = sunImage)
img1.pack()
img1.image = sunImage
def mercInfo():
textFrame.destroy()
mercImage = PhotoImage(file=mercPic)
img2 = Label(textFrame, image = mercImage).pack()
img2.image = mercImage
sun = Button(buttonFrame, text="THE SUN",command=sunInfo)
sun.pack(side=TOP)
mercury = Button(buttonFrame, text="MERCURY",command=mercInfo)
mercury.pack(side=TOP)
使用该代码,当我单击太阳或水星按钮时,我会收到此错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Python33libtkinter__init__.py", line 1475, in __call__
return self.func(*args)
File "C:UsersRyanDesktoppythonProjects for ParliamentuniverseinfoUniverse.py", line 30, in sunInfo
img1 = Label(textFrame, image = sunImage)
File "C:Python33libtkinter__init__.py", line 2596, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:Python33libtkinter__init__.py", line 2075, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: bad window path name ".45448776"
什么是错误/什么导致了此错误?
编辑:
我已经更改了它,因此功能如下所示:
def sunInfo():
textFrame.pack_forget()
sunImage = PhotoImage(file=sunPic)
img1 = Label(textFrame, image = sunImage)
img1.image = sunImage
img1.pack()
def mercInfo():
textFrame.pack_forget()
mercImage = PhotoImage(file=mercPic)
img2 = Label(textFrame, image = mercImage)
img2.image = mercImage
img2.pack()
但是,每当我运行程序并按下按钮时,图像都不会出现。
导致错误的行前面两行,你销毁textFrame
。然后,您尝试使用 textFrame
作为img1
的父级。小部件一旦被销毁,就无法使用。