[source code] Python GUI with Tkinter - 14 - Images and Icons

+1 Bucky Roberts · September 16, 2014

from tkinter import *

root = Tk()

photo = PhotoImage(file="banana.png")
label = Label(root, image=photo)
label.pack()

root.mainloop()

Post a Reply

Replies

Oldest  Newest  Rating
0 Jakob Jensen · October 5, 2014
Hi guys.

I am trying to insert a image on a button, but it does not work for some reason.

this is my code:
from tkinter import *

def doNothing():
    print('okay i wont')


root = Tk()

imageIcon = PhotoImage(file='camera.png')

menu = Menu(root)
root.config(menu=menu)#lav noget, lav en menu som er menu

subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)#cascade betyder dropdown menu

subMenu.add_command(label="Add Item", command=doNothing)#tilføjer punkt til menuen
subMenu.add_separator()
subMenu.add_command(label="Preferences", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing)

editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Copy", command=doNothing)

toolbar = Frame(root, bg='blue')
toolbutton1 = Button(toolbar, image=imageIcon, command=doNothing)
toolbutton1.pack(side=LEFT, padx=2, pady=2)#pad x/y er størrelsen på knappen
toolbutton2 = Button(toolbar, text='Print', command=doNothing)
toolbutton2.pack(side=LEFT, padx=2, pady=2)

toolbar.pack(side=TOP, fill=X,)


status = Label(root, text="Welcome", bd=1, relief=SUNKEN, anchor=W)#relief er design til dit label
status.pack(side=BOTTOM, fill=X)


root.mainloop()


And this is the error i get:

Traceback (most recent call last):
  File "/Users/jakobjensen/PycharmProjects/PythonSchool/PythonSchoolGUI/standartProgram.py", line 9, in <module>
    imageIcon = PhotoImage(file='camera.png')
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/__init__.py", line 3384, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/__init__.py", line 3340, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "camera.png"

Hope someone can help me solve this problem.

- Jakob Jensen
  • 1

Python

107,107 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator