Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108928

Re: Image loading problem

From Terry Reedy <tjreedy@udel.edu>
Newsgroups comp.lang.python
Subject Re: Image loading problem
Date 2016-05-21 15:23 -0400
Message-ID <mailman.87.1463858635.27390.python-list@python.org> (permalink)
References <743e5fbe-3a6c-49ea-ac5e-9bc055f652b5@googlegroups.com> <nhq3sl$hv8$1@ger.gmane.org> <nhqck1$e6c$1@ger.gmane.org>

Show all headers | View raw


On 5/21/2016 12:54 PM, Peter Otten wrote:
> sweating_ant@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()
>>
>> Somebody can help me out? Thanks!
>
> It's not your fault, there's an odd quirk in the library: you have to keep a
> reference of the PhotoImage instance around to prevent the image from being
> garbage-collected. For example
>
> from Tkinter import *
>
> def load_img(win):
>     global img
>
>     img = PhotoImage(file="xxx.gif")
>     Label(win, image=img).pack()
>
> win = Tk()
> load_img(win)
> win.mainloop()
>
> will work because img is now a global that is not deleted until the script
> ends (but don't run the function twice -- then the first image will
> disappear).

The usual procedure is to create an App class and make images attributes 
of the App instance.  Then load_img would be a method and the first line 
would be 'self.img = ...'.  One can also have a list of images, to use 
in a slide show, or a dict of images, so users can select.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Image loading problem "sweating_ant@yahoo.com" <huey.y.jiang@gmail.com> - 2016-05-21 08:22 -0700
  Re: Image loading problem Peter Otten <__peter__@web.de> - 2016-05-21 18:54 +0200
  Re: Image loading problem Peter Pearson <pkpearson@nowhere.invalid> - 2016-05-21 17:10 +0000
  Re: Image loading problem Gary Herron <gherron@digipen.edu> - 2016-05-21 10:16 -0700
  Re: Image loading problem Terry Reedy <tjreedy@udel.edu> - 2016-05-21 15:23 -0400
  Re: Image loading problem Random832 <random832@fastmail.com> - 2016-05-21 15:55 -0400
  Re: Image loading problem huey.y.jiang@gmail.com - 2016-05-21 21:01 -0700
  Re: Image loading problem Peter Otten <__peter__@web.de> - 2016-05-22 11:03 +0200
  Re: Image loading problem Michael Torrie <torriem@gmail.com> - 2016-05-22 13:37 -0600
  Re: Image loading problem Random832 <random832@fastmail.com> - 2016-05-22 16:19 -0400
    Re: Image loading problem Christian Gollwitzer <auriocus@gmx.de> - 2016-05-22 22:57 +0200

csiph-web