Seite 1 von 1

Tkinter Button mit Bild (.gif oder .jpg)

Verfasst: Mittwoch 19. November 2014, 18:56
von schnibli
Hallo zusammen,

Ich lese des öfteren in diesem Forum Beiträge, ich konnte auch schon vieles finden.
Leider habe ich nun ein Problem das so noch nicht behandelt wurde, daher habe ich mir einen Account erstellt.

Ich hoffe ich Poste es in das richtige Thead.

Ich würde gerne einen Button mit Bild in mein Gui importieren dazu habe ich Test-weise folgenden Code:

Code: Alles auswählen

gifdir = "./"
from tkinter import *
win = Tk()
igm = PhotoImage(file=gifdir+"logo.gif")
Button(win, image=igm).pack()
win.mainloop()
Jedoch erhalte ich folgenden Fehler:
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "C:\Python34\test\test.py", line 4, in <module>
igm = PhotoImage(file=gifdir+"logo.gif")
File "C:\Python34\lib\tkinter\__init__.py", line 3384, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python34\lib\tkinter\__init__.py", line 3340, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "./logo.gif": no such file or directory
>>>
Python Version steht ja oben: 3.4.1

Ich währe froh wenn mir jemand helfen könnte :)

Re: Tkinter Button mit Bild (.gif oder .jpg)

Verfasst: Mittwoch 19. November 2014, 18:59
von BlackJack
@schnibli: Offensichtlich liegt die Bilddatei nicht im Arbeitsverzeichnis des Prozesses.

Re: Tkinter Button mit Bild (.gif oder .jpg)

Verfasst: Mittwoch 19. November 2014, 19:10
von schnibli
Hab nun die Datei gelöscht und neu erstellt jetzt kommt ein anderer Fehler :s
Traceback (most recent call last):
File "C:\Python34\test\test.py", line 4, in <module>
photo=PhotoImage(file="Press1.gif")
File "C:\Python34\lib\tkinter\__init__.py", line 3384, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python34\lib\tkinter\__init__.py", line 3340, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "Press1.gif"
>>>
hab es gerade herausgefunden, ein Fehler in der Datei, Bild neu konvertiert, jetzt Funktioniert es.

Gibt es auch eine möglichkeit .jpg anzuzeigen?

Re: Tkinter Button mit Bild (.gif oder .jpg)

Verfasst: Mittwoch 19. November 2014, 19:36
von Ene Uran
Du brauchst PIL (Python Image Library) dazu:

Code: Alles auswählen

# display an image using Tkinter and PIL
# use a canvas so you can delete the image (eg. slide show) 
# PIL allows Tkinter to read more than just .gif image files

from PIL import ImageTk
try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

root = tk.Tk()

# pick an image file you have in your working directory
# or specify full path
image_file = "Beach.jpg"
photo = ImageTk.PhotoImage(file=image_file)
root.title(image_file)

# put the image on a canvas
cv = tk.Canvas()
cv.pack(side='top', fill='both', expand='yes')
img = cv.create_image(0, 0, image=photo, anchor='nw')
cv.update()

# optionally delete image after 5000 ms
cv.after(5000, cv.delete(img))
cv.update()

root.mainloop()

Re: Tkinter Button mit Bild (.gif oder .jpg)

Verfasst: Mittwoch 19. November 2014, 21:41
von schnibli
Vielen Dank :)