Reputation:
I want to display an icon on a menu bar, so using this information, I coded this:
img = Image.open("help.png")
menubar.add_cascade(label="Help",menu=helpmenu,bitmap=ImageTk.PhotoImage(img))
I got this error:
Traceback (most recent call last):
File "mine.py", line 67, in <module>
m.menus(root)
File "mine.py", line 55, in menus
menubar.add_cascade(label="Help",menu=helpmenu,bitmap=ImageTk.PhotoImage(img))
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2699, in add_cascade
self.add('cascade', cnf or kw)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2696, in add
self._options(cnf, kw))
_tkinter.TclError: bitmap "pyimage2" not defined
How to fix this ?
Upvotes: 4
Views: 3132
Reputation: 10532
To display a PhotoImage
you should use the image
attribute, not bitmap
.
You can also simply open an image file directly using ImageTk.PhotoImage(file='...')
So you can use the following code to display your image in the menu:
img = ImageTk.PhotoImage(file="help.png")
menubar.add_cascade(label="Help", menu=helpmenu, image=img)
Upvotes: 1