[英]Image not displaying in Tkinter Label widget
我正在嘗試讓圖像顯示在 Tkinter 標簽小部件中。 這段代碼在 PyCharm 的一個類中工作,但不會超過主應用程序中的“tk.Label”行。 我在這里咨詢了其他答案,但無法弄清楚為什么圖像沒有顯示在主應用程序中。
logo_filepath = "/Users/xxx/MASTER/pymol/Master/cache/logos/tmpbhWv2Ts.gif"
self.img = tk.PhotoImage(file = logo_filepath)
self.logo = tk.Label(self, image=self.img)
self.logo.photo = self.img
self.logo.grid(row=0, column=3, rowspan=10, columnspan=4)
這是一個非常簡單的錯誤。 只要確保您沒有在類之外定義self.[Your Variable]
。 因為self
只在類中可用。 另外,這是我的代碼:
import Tkinter as tk
root = tk.Tk()
logo_filepath = "Your File Path..."
img = tk.PhotoImage(file = logo_filepath)
logo = tk.Label(root, image=img)
logo.photo = img
logo.grid(row=0, column=3, rowspan=10, columnspan=4)
tk.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.