问题:python Tkinter实时更改label里的图片?
代码如下:
from tkinter import* from PIL import ImageTk from PIL.ImageTk import PhotoImage def abc(): img = ImageTk.PhotoImage(file="000888.jpg") label.configure(image = img) root=Tk() img = ImageTk.PhotoImage(file="000581.jpg") label=Label(root, image=img, compound=CENTER) label.grid(row=1) buttun01=Button(root,text="change",command=abc) buttun01.grid(row=2) mainloop()
以上的代码运行的效果是一片空白,内容不对,修改后的代码如下所示:
如果不用PIL库,直接下面这样是可行的,没用过PIL库,不过tkinter里面的PhotoImage(file='000581.jpg'),文件不能是jpg格式,要改成png格式的图片
from tkinter import*def abc(): label.config(image = img2)root=Tk() img = PhotoImage(file='000581.png') #改成png格式的图片 img2=PhotoImage(file='000888.png') label=Label(root,image=img,compound=CENTER) label.grid(row=1)buttun01=Button(root,text="change",command=abc) buttun01.grid(row=2) mainloop(