簡體   English   中英

Python 標簽圖像不存在,但 os.path 說它存在

[英]Python Label image doesn't exist but os.path says it does

這是讓我發瘋的代碼:

from tkinter import*
import os.path
class About:
    def __init__(self):
        font1='tahoma 12'
        win=Tk()
        print(os.path.isfile('logo.gif'))#It returns true
        Label(win,image="logo.gif").pack()                
About()
Label(win,image="logo.gif").pack()

image參數不接受文件名。 根據教程,“該值應該是 PhotoImage、BitmapImage 或兼容對象。” 它繼續討論PhotoImage類,您應該使用它。

您可以使用標簽來顯示 PhotoImage 和 BitmapImage 對象。 執行此操作時,請確保保留對圖像對象的引用,以防止 Python 的內存分配器對其進行垃圾回收。 您可以使用全局變量或實例屬性,或者更簡單,只需向小部件實例添加一個屬性:

photo = PhotoImage(file="icon.gif")
w = Label(parent, image=photo)
w.photo = photo
w.pack()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM