0

I'm trying to create an image, but it's not working and I'm not sure what I'm doing wrong. All I get when I click the button that should display the image is the white canvas, without the image. How do I get it to display?

def show_original(self):

    from os.path import exists
    from PIL import Image, ImageTk


    if not os.path.exists(self.wdg_orig_file_name_.get()):
        tkMessageBox.showinfo('Load','File does not exist:' + self.wdg_orig_file_name_.get())

        return
    self.orig_image_=Image.open(str_orig_file_name)
    canvas = self.gui_.ca(500,500,bg='white')
    im_TK = ImageTk.PhotoImage(self.orig_image_)
    canvas.create_image(250,250,image=im_TK)
    canvas.pack()
    pass

self.wdg_orig_file_name_.get() in the main loop is:

 self.wdg_orig_file_name_ = self.gui_.en(text='boat.png')

The global str_orig_file_name is assigned in pick_file():

def pick_file(self):
    '''Opens a file dialog and sets its result to the filename entry'''
    global str_orig_file_name
    str_orig_file_name = tkFileDialog.askopenfilename()
    if str_orig_file_name:
        self.wdg_orig_file_name_.delete(0, END)
        self.wdg_orig_file_name_.insert(0, str_orig_file_name)

        #We got a new image to process. Forget the previous results.
        self.orig_image_ = None
        self.preview_image_ = None
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • 1
    So what is your question? Do you get an error? What exactly doesn't work? – cel Jun 11 '15 at 18:38
  • Sorry. i edited my question. – Sharon Tarrab Jun 11 '15 at 18:43
  • 1
    It looks like you're already using `tkinter` as your GUI toolkit, correct? And have you tried saving the final `PhotoImage` object `im_TK` as `self.im_TK` instead, so that the reference doesn't get garbage-collected? – TigerhawkT3 Jun 11 '15 at 19:13
  • thanks for your reply. I'm a Python beginner and to be honest I'm not sure if I imported the right libs. I thought PIL is the one I need here, but according to your comment I tried to `import tkinter` and got error which I have `no moudle name tkinter`. it's probably it. now I guess my next question should be "how do I install tkinter in Portable Python" :( – Sharon Tarrab Jun 11 '15 at 19:38
  • 1
    Well, if it's running Python 2, then you need to `import Tkinter` with a capital `T`. If it just plain doesn't include any flavor of `tkinter`, you're in for some fun (I still haven't figured out how to do it on my Ubuntu live USB setup). – TigerhawkT3 Jun 11 '15 at 19:51
  • you right. `import Tkinter` with capital letter worked. I feel so noob. now I'm confused. – Sharon Tarrab Jun 11 '15 at 20:00

2 Answers2

-1

Have you tried easygui?

#!/usr/bin/python

from easygui import *

image = "img.jpg"
msg   = "Do you like this picture?"
choices = ["Yes","No","No opinion"]
reply=buttonbox(msg,image=image,choices=choices)

Very easy.

http://www.ferg.org/easygui/tutorial.html

Alex Ivanov
  • 695
  • 4
  • 6
  • 1
    The OP didn't clearly specify it, but it looks like they're using `tkinter` widgets and functions, so I don't think they're looking for GUI recommendations (which would be off-topic anyway). – TigerhawkT3 Jun 11 '15 at 19:17
-1

I'm sorry. I though the OP was looking for a way to display an image and now I see the image is supposed to be on the button. Well I tried this and it works just fine with .gif and .png. However .jpg gives an error: "_tkinter.TclError: couldn't recognize data in image file "img.jpg"".

#!/usr/bin/python

from Tkinter import *
root=Tk()
b=Button(root,justify = LEFT)
photo=PhotoImage(file="img.png")
b.config(image=photo,width="100",height="100")
b.pack(side=LEFT)
root.mainloop()
Alex Ivanov
  • 695
  • 4
  • 6