Path: csiph.com!news.mixmin.net!news.unit0.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Gary Herron Newsgroups: comp.lang.python Subject: Re: Image loading problem Date: Sat, 21 May 2016 10:16:48 -0700 Lines: 57 Message-ID: References: <743e5fbe-3a6c-49ea-ac5e-9bc055f652b5@googlegroups.com> <57409800.3090401@digipen.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de mtndiMAc8h9aEWWa40WVfQuUoGZxBEsSet1omvslBySQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.069 X-Spam-Evidence: '*H*': 0.86; '*S*': 0.00; 'def': 0.13; 'loaded.': 0.16; 'main().': 0.16; 'mean,': 0.16; 'out?': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'tk()': 0.16; 'wrote:': 0.16; 'creates': 0.18; 'project,': 0.18; 'variable': 0.18; 'load': 0.20; 'all,': 0.20; 'function,': 0.22; 'subject:problem': 0.22; 'tkinter': 0.22; 'trying': 0.22; 'am,': 0.23; 'references': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'function': 0.28; 'img': 0.29; 'code:': 0.29; 'somebody': 0.30; 'window': 0.30; 'are:': 0.32; 'problem': 0.33; 'thanks!': 0.34; 'so,': 0.35; 'label': 0.35; 'but': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'display': 0.37; 'skip:p 20': 0.38; 'goes': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'charset:windows-1252': 0.62; 'email addr:yahoo.com': 0.63; 'believe': 0.66; 'dr.': 0.69; 'received:204': 0.75; 'institute': 0.77; '(425)': 0.84; '895-4418': 0.84; 'digipen': 0.84; 'herron': 0.84; 'returns.': 0.84; 'tkinter,': 0.84 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 In-Reply-To: <743e5fbe-3a6c-49ea-ac5e-9bc055f652b5@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <57409800.3090401@digipen.edu> X-Mailman-Original-References: <743e5fbe-3a6c-49ea-ac5e-9bc055f652b5@googlegroups.com> Xref: csiph.com comp.lang.python:108927 On 05/21/2016 08:22 AM, sweating_ant@yahoo.com wrote: > Hi All, > > > 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! > I believe this the problem (However It's been long since I used Tkinter, so be warned ... ): The function load_img creates a local variable named img which goes out of scope and is deleted immediately when the function returns. However, Tkinter needs you to keep that image around as long as the Label uses it. So, some solutions are: keep_me = [] # Global for keeping references to images def load_img(win): img = PhotoImage(file="xxx.gif") keep_me.append(img) Label(win, image=img).pack() or def load_img(win): img = PhotoImage(file="xxx.gif") Label(win, image=img).pack() return img saved_img = load_img(win) ... Gary Herron -- Dr. Gary Herron Professor of Computer Science DigiPen Institute of Technology (425) 895-4418