1

I have a simple GUI program I'm building with Tkinter. I want to have a bunch of buttons with app images on them that, when clicked, launch that app. The problem is, Python can't recognise the Skype.gif file.

Code:

from tkinter import *
import os

def open_skype():
    os.system('open /Applications/Skype.app')

master = Tk()

photo = PhotoImage(file='/Users/michael/Desktop/Skype.gif')

but = Button(master, image=photo, command=open_skype)



objs = [but]

column = 1
for i in objs:
    i.grid(column=column, row=1)
    column += 1
mainloop()

Error message:

_tkinter.TclError: couldn't recognize data in image file "/Users/michael/Desktop/Skype.gif"

woff
  • 107
  • 1
  • 11
  • Use image in `PNG` or `JPG` format. – furas Jul 13 '14 at 16:24
  • I tried those and I got the same error: _tkinter.TclError: couldn't recognize data in image file "/Users/michael/Desktop/Skype.png" – woff Jul 13 '14 at 16:25
  • How did you convert it from `GIF` to `PNG` ? Can you see this image in some image viewer ? – furas Jul 13 '14 at 16:26
  • Definitely converted it, and it isn't corrupt. – woff Jul 13 '14 at 16:28
  • Can you see this image in some image viewer ? – furas Jul 13 '14 at 16:28
  • Yeah, preview on mac. – woff Jul 13 '14 at 16:29
  • Probably you will need install and import another module for images - see [PhotoImage](http://effbot.org/tkinterbook/photoimage.htm) in [Tkinterbook](http://effbot.org/tkinterbook/) – furas Jul 13 '14 at 16:35
  • have you checked this one? http://stackoverflow.com/questions/15718663/tkinter-image-not-showing-or-giving-an-error – Lafexlos Jul 13 '14 at 16:47
  • Try with this image http://furas.pl/screenshot.gif It works for me. If it doesn't work for you maybe you need some library in system or module in python. – furas Jul 13 '14 at 16:48
  • You can try in console/terminal `file Skype.gif` - maybe it give you some inforamtion about file format. – furas Jul 13 '14 at 16:51
  • Yeah, that image doesn't work, and in terminal it replies: Skype.gif: data – woff Jul 13 '14 at 16:52
  • Not directly connected to the problem but `objs` and the loop look very strange and unnecessarily complex. `but.grid(column=1, row=1)` has the same effect. BTW counting of rows and columns starts at 0. You are leaving cells in that grid unused. – BlackJack Jul 13 '14 at 16:55
  • `file screenshot.gif` gives me `screenshot.gif: GIF image data, version 89a, 809 x 775` so `file Skype.gif` should give you something similar. – furas Jul 13 '14 at 16:57
  • @BlackJack, I'm planning to do this with lots of buttons and I just wrote that out so I could add to the array as I wanted. Also, the way I did it nicely keeps each widget on the same row, and lays them out as they are in the array. Column is for future use. Furas, it only replies Skype.gif: data. I'm on a mac if that helps. – woff Jul 13 '14 at 16:57
  • Did you try `file screenshot.gif` ? Well, maybe you will have to install `PIL` to work with other image formats - see `Tkinterbook` again. – furas Jul 13 '14 at 17:02
  • The way you did it every widget in `objs` would go into the very same cell. Maybe you meant to write `for i, wigdet in enumerate(objs):` and use `i` as value for `column`, the point is that's half baked code irrelevant to the problem. – BlackJack Jul 13 '14 at 17:02
  • Ah, I didn't paste column += 1 – woff Jul 13 '14 at 17:05
  • `enumerate(objs)` or `enumerate(objs,1)` is good idea :) better then `column += 1` – furas Jul 13 '14 at 17:07
  • Nah, it's fine - my code works. I see why it's better though. – woff Jul 13 '14 at 17:08

4 Answers4

0

Your problem is most likely that the image is not in the correct place. TO ensure that it is, try entering your command line (Terminal for Macs), and typing in ls /Users/michael/Desktop/Skype.gif. If prints Skype.gif or /Users/michael/Desktop/Skype.gif, then the file is there, otherwise it is not.

ZenOfPython
  • 891
  • 6
  • 15
0

The path starts at where the python file is located so if they're in the same folder just put photo = PhotoImage(file='Skype.gif')

0

Tkinter only recognize s PNG images or JPG images in some cases. If you have a gif image you can incorporate it by using another module called WxPython. How this works is that it uses Frame by frame images to display a video type image. Tkinter does not support this.

XzibitGG
  • 330
  • 5
  • 18
0

Make sure your image is in the format of PNG or JPG

Jack Carter
  • 47
  • 1
  • 8