from tkinter import *
from tkinter.filedialog import *
wd = Tk()
def func_open():
fname = askopenfilename(parent=wd, filetypes=( ("gifs","*.gif"), ("alls","*.*") ))
photo1 = PhotoImage( file = fname )
pLabel.configure( image = photo1 )
pLabel.image=photo1
temp = PhotoImage()
pLabel = Label(wd, image = temp )
pLabel.pack(expand=1)
mainMenu = Menu(wd)
wd.config(menu=mainMenu)
fileMenu = Menu(mainMenu)
mainMenu.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="Open",command=func_open)
wd.mainloop()
上面的 2 行代碼,
pLabel.configure( image = photo1 )
pLabel.image=photo1
如果我洗掉其中之一,func_open()則無法列印影像檔案。
對我來說,似乎兩條線都說了同樣的話,
因為pLabel.configure( image = photo1 ) 將影像放入引數 photo1
并pLabel.image=photo1直接將 photo1 放入 pLabel 的影像。
我嘗試搜索該 .configure() 方法,但我無法獲得任何可以理解的資訊。
uj5u.com熱心網友回復:
小部件具有控制其外觀和行為的內部配置選項。當您呼叫該configure方法時,您正在為其中一個選項賦予一個值。因此,pLabel.configure( image = photo1 )將image選項設定為photo1變數的值。
當您這樣做時,您將在命名的 python 物件pLabel.image=photo1上創建一個新的實體變數并將其設定為變數的值。底層的 tkinter 小部件不知道這個屬性,并且不受這個屬性的影響。pLabelimagephoto1
這是保存對影像的參考的常用習慣用法。這個詞的使用image是完全任意的,使用pLabel.xyzzy=photo1或pLabel.save_this=photo1將解決完全相同的問題。
有關更多資訊,請參閱為什么在函式中創建 Tkinter 影像不顯示?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/465734.html
下一篇:從另一個模塊傳遞變數時出錯
