Image loading problem

Peter Pearson pkpearson at nowhere.invalid
Sat May 21 13:10:03 EDT 2016


On Sat, 21 May 2016 08:22:41 -0700 (PDT), sweating_ant at yahoo.com wrote:
>
> I am working on an image project, and I can display my image in
> main(). I mean, I can load my image in my main(). Needless, it is
> awkward. I am trying to load my image with a function, but got an
> empty image window popped up, no image content loaded. Please take a
> look at code:
>
> rom Tkinter import *
>
> def load_img(win):	
> 	img = PhotoImage(file="xxx.gif")
> 	Label(win, image=img).pack()
> 	
> win = Tk()
> load_img(win)
> win.mainloop()
>

This seems to work:
-----------
from Tkinter import Tk, PhotoImage, Label

def load_img(win):
        img = PhotoImage(file="/home/peter/dow.gif")
        Label(win, image=img).pack()
        return img

win = Tk()
img = load_img(win)
win.mainloop()
-----------

I guess the problem was that the "img" created in load_img went
out of scope and was deleted when load_img returned.  I just tried
the "return img" as an experiment to keep it from going out of scope;
from a software-engineering standpoint this might be a gross kluge.

Baffling? Personally, I never got past feeling generally bewildered in
Tkinter-land, and now I use matplotlib for all my graphical needs.  I
had great hopes for PyGUI at one point, but I hear very little about it
now.

-- 
To email me, substitute nowhere->runbox, invalid->com.



More information about the Python-list mailing list