tkinter按钮不能更换图片
本帖最后由 Guru 于 2019-8-5 13:41 编辑debug可以看到点了按钮,确实读取了另一张图,但就是不显示出来。
update,sleep,after都试过了,图片还是不刷新。
求教是哪里的问题?
已找到错误:
不应在每次更换图片时重新执行lblImage = Label(frm, image = img),将其放置在drawui中只执行一次,以后的每次修改图片仅执行frm.configure(image = img)和frm.image = img即可。
#代码已大量删减
from tkinter import *
from urllib.request import urlopen
from PIL import Image, ImageTk
import os
from random import randint
#绘图
def drawpic():
if cata == 'PIC':
img= ImageTk.PhotoImage(pil_image_out)
lblImage = Label(frm, image = img)
lblImage.image = img
lblImage.grid(sticky=E+W)
window.update()
window.update_idletasks()
else:
pass
#改变图片
def change():
newid = currnum + changenum
if newid > (len(dir)-1):
newid = 0
elif newid < 0:
newid = (len(dir)-1)
drawpic()
print('Clicked')
window.update()
window.update_idletasks()
#初始化
def drawui():
window.title('TEST')
frmHATpic = Frame (width=200)
frmHATpic.grid_propagate(0)
#图片
drawpic()
#生成按钮
HAT_BACK_BUTTON = Button(window,text='back',command=lambda:change(-1))
HAT_NEXT_BUTTON = Button(window,text='next',command=lambda:change(1)
if __name__ == '__main__':
window = Tk()
drawui()
window.mainloop()
页:
[1]