2

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)
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
maicol07
  • 199
  • 1
  • 6
  • 16
  • Could you try and display one of the images in your main widget? Just add `Label(w, image=PhotoImage(r"icons\settings.png")).pack()` in your code. If it doesn't show up, then the problem lies in the image creation itself. – Right leg Aug 30 '17 at 12:02

1 Answers1

1

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.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • But I use images with the PhotoImage function also with other widgets... Note that I'm using Python 3.6.2 – maicol07 Aug 30 '17 at 13:28
  • png is support in the current release of tkinter. – Mike - SMT Aug 30 '17 at 13:36
  • true, I do not know which version the OP is using, but since he is not displaying them, I suppose his version is not that new. @SierraMountainTech – Billal Begueradj Aug 30 '17 at 13:37
  • I just noticed the OP commented he is using 3.6.2 and thought I would bring up that bit of info as it is relevant to the OPs question – Mike - SMT Aug 30 '17 at 13:45
  • In the same screen I have an other png image. In an another file I added three buttons each one with an image. The problem is that these image are not displayed... Now I'll share you the full code – maicol07 Aug 30 '17 at 14:03
  • Please test [this code](https://pastebin.com/u6DViT6L) and tell me if it works for you (you can change `hello.jpg` file with a PNG file) @MaicolBatti – Billal Begueradj Aug 30 '17 at 14:25
  • I think that Tkinter doesn't support jpg images... this is the error: – maicol07 Aug 30 '17 at 15:08
  • `Traceback (most recent call last): File "test.py", line 6, in photo = tk.PhotoImage(file="369975.jpg") File "C:\Users\Maicol\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 3539, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Users\Maicol\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 3495, in __init__ self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't recognize data in image file "369975.jpg"` – maicol07 Aug 30 '17 at 15:09
  • The error says your Tkinter version does not support PNG files. So you have no option other than my own solution or upgrading your Python/tkinter versions to the current ones @MaicolBatti – Billal Begueradj Aug 30 '17 at 17:38
  • No, I don't think that, I have a PNG image in the program in a Label and it displays well... – maicol07 Aug 31 '17 at 11:17
  • With the method in the answer I get this error: Traceback (most recent call last): File "C:\Users\Maicol\Documents\Projects\App_WINDOWS\School_Life_Diary\main.py", line 100, in image = Image.open("icons\exit.png") AttributeError: type object 'Image' has no attribute 'open' – maicol07 Sep 01 '17 at 14:04
  • For me it works ... use [this answer](https://stackoverflow.com/questions/10748822/img-image-openfp-attributeerror-class-image-has-no-attribute-open) to fix that error. @MaicolBatti – Billal Begueradj Sep 01 '17 at 14:16
  • Ok, now it works... but the icons are big!! Are there any method to scale them automaticly? Thanks – maicol07 Sep 01 '17 at 14:58
  • Resizing your icons is a classical problem. There are many posts about it on this website. For example [this one](https://stackoverflow.com/questions/4066202/resizing-pictures-in-pil-in-tkinter) @MaicolBatti – Billal Begueradj Sep 01 '17 at 15:04
  • I tried, but the image doesn't display... This is the code: `fm.add_command(label=_("Esci"), image=PIL.ImageTk.PhotoImage(PIL.Image.open("icons\exit.png").resize((32,32),PIL.Image.ANTIALIAS)), compound="left",command=w.destroy)` – maicol07 Sep 01 '17 at 15:06
  • maybe you made them too small? Try to see if any errors were thrown in case you did not code the resizing correctly. Debug your code, nothing is difficult, you can handle that yourself if you want to be a programmer @MaicolBatti – Billal Begueradj Sep 01 '17 at 15:09
  • Ok, thanks for the tip. I think the best thing to do (because the icons are only for the menu) is resize them externally with paint.net. Thanks for your help! However, Python allows this method with PIL only if the images are in different variables... I don't know why... but it works! – maicol07 Sep 01 '17 at 15:18
  • EDIT: I have found the problem: the resize must be effettued in more variables. Like this: https://pastebin.com/ULAZfSJm – maicol07 Sep 01 '17 at 15:28