maicol07
maicol07

Reputation: 301

Python Tkinter - Menu icon doesn't show

I'm building a menu with tkinter but the icons don't show. Can you help me?

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)

Upvotes: 2

Views: 1330

Answers (1)

Billal BEGUERADJ
Billal BEGUERADJ

Reputation: 22744

As explained here, for such images formats, you need to use the PIL library which converts them to Tkinter-compatible image objects:

from PIL import Image, ImageTk

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

Then attach it to your widget:

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

You need to repeat this process for each .png image you used.

Upvotes: 1

Related Questions