Seite 1 von 1
couldnt recognize data in image error
Verfasst: Samstag 24. Februar 2018, 17:55
von Tom12
heute muss ich wieder mal viel erfragen :K
ich will einen Button ein anderes image geben sobald man auf diesen drückt
Das ist der normale button
Button1 = tkinter.Button(root, text="j", command=Bomben1, height = 5, width = 10)
Button1.grid(row=1, column=0)
und in einen if clause habe ich diese Zeilen eingebaut
photo1 = tkinter.PhotoImage(file="wiese.jpg")
ph1 = tkinter.ImageTk.PhotoImage(photo1)
Button1.config(image=photo1)
nun bekomme ich immer den Fehler:
_tkinter.TclError: couldn`t recognize data in image file "wiese.jpg"
Das bild befindet sich im selben ordner als das Script er findet es also aber kann es nich auslesen wieso ?
Re: couldnt recognize data in image error
Verfasst: Samstag 24. Februar 2018, 18:50
von wuf
Hi Tom12
Zum anzeigen von Bildern im
jpg-Format brauchst du zusätzlich das
PIL-Modul (Python Image Library). Mit dem Tkinter eigenen
tk.PhotoImage lassen sich nur die Bildformate
gif und
png anzeigen. Mit PIL lassen sich alle Bildformate anzeigen.
Hier die Tkinter-Variante:
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import partial
try:
#~~ For Python 2.x
import Tkinter as tk
except ImportError:
#~~ For Python 3.x
import tkinter as tk
def bomben(button, button_image, event):
button.configure(image=button_image)
app_win = tk.Tk()
tk_button_image = tk.PhotoImage(file="wiese.png")
#tk_button_image = tk.PhotoImage(file="wiese.gif")
button = tk.Button(app_win, text="j")
button.pack(padx=10, pady=10)
button.bind('<Button-1>', partial(bomben, button, tk_button_image))
app_win.mainloop()
Hier die PIL-Variante:
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import partial
from PIL import Image, ImageTk
try:
#~~ For Python 2.x
import Tkinter as tk
except ImportError:
#~~ For Python 3.x
import tkinter as tk
def bomben(button, button_image, event):
button.configure(image=button_image)
app_win = tk.Tk()
pil_button_image = Image.open("wiese.jpg")
#pil_button_image = Image.open("wiese.gif")
#pil_button_image = Image.open("wiese.png")
tk_button_image = ImageTk.PhotoImage(pil_button_image)
button = tk.Button(app_win, text="j")
button.pack(padx=10, pady=10)
button.bind('<Button-1>', partial(bomben, button, tk_button_image))
app_win.mainloop()
Gruss wuf
Re: couldnt recognize data in image error
Verfasst: Samstag 24. Februar 2018, 20:13
von Tom12
wuf hat geschrieben:Hi Tom12
Zum anzeigen von Bildern im
jpg-Format brauchst du zusätzlich das
PIL-Modul (Python Image Library). Mit dem Tkinter eigenen
tk.PhotoImage lassen sich nur die Bildformate
gif und
png anzeigen. Mit PIL lassen sich alle Bildformate anzeigen.
Gruss wuf
Danke jetzt bekomme ich keinen Fehler mehr allerdings schaut der Button nun aus wie eine weiße box und hat nicht das Image des Bildes.
hier der Code.
photo1 = tkinter.PhotoImage(file="wiese.png")
Button1.config(image=photo1, width="75", height="78")
An was könnte es liegen
Re: couldnt recognize data in image error
Verfasst: Samstag 24. Februar 2018, 21:30
von wuf
Hi Tom12
Du musst für
width und
height des Buttons keine Stringwerte übergeben. Bei mir wird der Button auf die richtige Grösse skaliert (75x78 pixels). Welche Geometrie hat dein Bild (wiese.png)? Wenn du absolute Werte für
widh &
height des Button angibst muss das anzuzeigende Bild gleich gross oder kleiner sein. Das Bild wird nicht automatisch skaliert.
Probiere das folgende Skript:
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import partial
try:
#~~ For Python 2.x
import Tkinter as tk
except ImportError:
#~~ For Python 3.x
import tkinter as tk
def bomben(button, button_image, event):
button.configure(image=button_image, width=75, height=78)
app_win = tk.Tk()
tk_button_image = tk.PhotoImage(file="wiese.png")
#tk_button_image = tk.PhotoImage(file="wiese.gif")
button = tk.Button(app_win, text="j")
button.pack(padx=10, pady=10)
button.bind('<Button-1>', partial(bomben, button, tk_button_image))
app_win.mainloop()
Gruss wuf