這個問題在這里已經有了答案: 如果在函式中創建,為什么 Tkinter 影像不顯示? (3 個回答) 昨天關門。
我正在嘗試將影像加載到 tkinter 中,以用作背景影像。該影像是 GIF(最初為 JPG,但我聽說 tkinter 不支持該格式),其尺寸與視窗相同。無論如何,當我運行我的代碼時,它運行了,但是 tkinter 視窗是空的!這是我的視窗代碼:
class Window(Tk):
def __init__(self):
super().__init__()
self.geometry("700x600")
self.resizable(False, False)
def background_img(self, img_path):
background_img = PhotoImage(file=img_path)
background_img_label = Label(self, image=background_img)
background_img_label.place(x=0, y=0)
window = Window()
img_path = "background.gif"
window.background_img(img_path)
window.mainloop()
你能告訴我我做錯了什么嗎?提前致謝。
uj5u.com熱心網友回復:
正如TDG所說,名稱background_img(function) 和background_img(tkinter.PhotoImage) 相互覆寫。你應該重命名一個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/434006.html
