以下是所有代码。但我不明白有关的函数定义的两行代码,pLabel.configure(image = photo)
和pLabel.image = photo
。这两行是什么意思?当我在Google上搜索它时,它说“保持参考”,但是我不能完全理解“保持参考”。
from tkinter import *
from time import *
fnameList = ["test1.gif", "test2.gif", "test3.gif", "test4.gif", "test5.gif",
"test6.gif", "test7.gif", "test8.gif", "test9.gif",]
photoList = [None] * 9
num = 0
def clickNext() :
global num
num += 1
if num > 8 :
num = 0
photo = PhotoImage(file = "chapter10/gif/" + fnameList[num])
pLabel.configure(image = photo)
pLabel.image = photo
def clickPrev() :
global num
num -= 1
if num < 0 :
num = 8
photo = PhotoImage(file = "chapter10/gif/" + fnameList[num])
pLabel.configure(image = photo)
pLabel.image = photo
window = Tk()
window.geometry("700x500")
window.title("album")
btnPrev = Button(window, text = "<< prev", command = clickPrev)
btnNext = Button(window, text = "next >>", command = clickNext)
window.bind("<Up>", clickNext) # PageUp key click
window.bind("<Down>", clickPrev) # PageDown key click
photo = PhotoImage(file = "chapter10/gif/" + fnameList[0])
pLabel = Label(window, image = photo)
btnPrev.place(x = 250, y = 10)
btnNext.place(x = 400, y = 10)
pLabel.place(x = 15, y = 50)
window.mainloop()
第一行是配置Label
窗口小部件以显示PhotoImage
对象。第二行通过显式添加该图像对象的引用作为小部件的命名属性image
来存储对该图像对象的引用Label
。这样一来,PhotoImage
当函数返回时,不会自动对对象进行垃圾回收(因为这photo
是两个函数中的局部变量)。
之所以需configure()
要这样做,是因为widget方法不能做到这一点(正如人们所期望的那样,因为这通常是在Python中工作的方式-但tkinter
有所不同)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句