2017-08-30 93 views
2

我正在用tkinter構建一個菜單,但圖標不顯示。 你能幫我嗎?Python Tkinter - 菜單圖標不顯示

mb=Menu(w) 
w.config(menu=mb) 
fm=Menu(mb,tearoff=0) 
om=Menu(mb,tearoff=0) 
hm=Menu(mb,tearoff=0) 
mb.add_cascade(label=_("File"),menu=fm) 
fm.add_command(label=_("Esci"), image=PhotoImage(r"icons\exit.png"), 
       compound="left",command=w.destroy) 
fm.iconPhotoImage = PhotoImage(r"icons\exit.png") 
mb.add_cascade(label=_("Opzioni"),menu=om) 
om.add_command(label=_("Impostazioni"), image=PhotoImage(r"icons\settings.png"), 
       compound="left", command=settings.creaFinestra) 
om.add_command(label=_("Cambia lingua"), image=PhotoImage(r"icons\language.png"), 
       compound="left", command=settings.cambiaLingua) 
mb.add_cascade(label=_("Aiuto"), menu=hm) 
hm.add_command(label=_("Guida"), image=PhotoImage(r"icons\help1.png"), 
       compound="left", 
       command= lambda: webbrowser.open("https://github.com/maicol07/school_life_diary_pc/wiki")) 
hm.add_command(label=_("Informazioni"), image=PhotoImage(r"icons\help.png"), 
       compound="left",command=info) 
+0

你可以嘗試在主小部件中顯示其中一個圖像嗎?只需在代碼中添加'Label(w,image = PhotoImage(r「icons \ settings.png」))。pack()'。如果它沒有出現,那麼問題在於圖像創建本身。 –

回答

1

至於解釋here,對於這樣的圖像格式,你需要使用PIL庫,將它們轉換成Tkinter的兼容圖像對象

from PIL import Image, ImageTk 

image = Image.open("icons\exit.png") 
photo = ImageTk.PhotoImage(image) 

然後將其連接到你的widget:

fm.add_command(label=_("Esci"), image=photo, ...) 

您需要爲每個使用的.png圖像重複此過程。

+0

但我使用PhotoImage功能的圖像也與其他小工具...請注意,我使用Python 3.6.2 –

+0

PNG是當前版本的tkinter支持。 –

+0

是真的,我不知道OP使用哪個版本,但由於他沒有顯示它們,我想他的版本不是那麼新。 @SierraMountainTech –