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


Groups > comp.lang.python > #108928

Re: Image loading problem

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Terry Reedy <tjreedy@udel.edu>
Newsgroups comp.lang.python
Subject Re: Image loading problem
Date Sat, 21 May 2016 15:23:42 -0400
Lines 50
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>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de JMB6zAFtiChdiPxlwIzcPwRyenUDdUf8mgwfB1iCNEHA==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'attributes': 0.07; 'dict': 0.09; 'instance.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'jan': 0.11; 'def': 0.13; '(but': 0.15; 'loaded.': 0.16; 'main().': 0.16; 'mean,': 0.16; 'out?': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'select.': 0.16; 'tk()': 0.16; 'wrote:': 0.16; 'app': 0.16; 'odd': 0.18; 'project,': 0.18; 'load': 0.20; 'prevent': 0.20; 'function,': 0.22; 'subject:problem': 0.22; 'tkinter': 0.22; 'trying': 0.22; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'script': 0.25; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'function': 0.28; 'img': 0.29; 'code:': 0.29; 'somebody': 0.30; 'window': 0.30; 'run': 0.33; 'class': 0.33; 'thanks!': 0.34; 'list': 0.34; 'instance': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'display': 0.37; 'method': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'to:addr:python.org': 0.40; 'your': 0.60; 'email addr:yahoo.com': 0.63; 'received:96': 0.63; 'otten': 0.84; 'received:fios.verizon.net': 0.91
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host pool-96-227-207-81.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0
In-Reply-To <nhq3sl$hv8$1@ger.gmane.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <nhqck1$e6c$1@ger.gmane.org>
X-Mailman-Original-References <743e5fbe-3a6c-49ea-ac5e-9bc055f652b5@googlegroups.com> <nhq3sl$hv8$1@ger.gmane.org>
Xref csiph.com comp.lang.python:108928

Show key headers only | 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