这是我的简单代码:-
import tkinter
from tkinter import *
from PIL import Image,ImageTk
def main():
body=Tk()
Top=Frame(body)
global im,tk
im=Image.open('kiit200.jpg')
tk=ImageTk.PhotoImage(im)
ilab=Label(body,image=tk)
ilab.pack()
def button():
body2=Tk()
Top2=Frame(body)
l2=Label(Top2,text="Success")
l2.pack()
ilab2=Label(body2,image=tk)
ilab2.pack()
mainloop()
l=Button(command=button,text="test")
l.pack()
mainloop()
main()
当我尝试在button方法中分配相同的图像时,其获取 错误:
File "C:\Users\My Love\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\My Love\Desktop\New Folder\Unknown_Project\Unknown_package\test.py", line 20, in button
ilab2=Label(body2,image=tk)
File "C:\Users\My Love\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2763, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\My Love\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2296, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist