[Tutor] newbie help..image in toplevelwindow?

a.dijkstra03@kpn.com a.dijkstra03@kpn.com
Tue, 12 Jun 2001 13:38:53 +0200


YESS. It works! Thank you very much.

-----Oorspronkelijk bericht-----
Van: Michael P. Reilly
[mailto:arcege@dsl092-074-184.bos1.dsl.speakeasy.net]
Verzonden: dinsdag 12 juni 2001 13:35
Aan: a.dijkstra03@kpn.com
CC: tutor@python.org
Onderwerp: Re: [Tutor] newbie help..image in toplevelwindow?


a.dijkstra03@kpn.com wrote
> This is one of my first attempts building a application with Tkinter.
> Could anyone explain to me what i'm doing wrong? I'm trying to insert a
> image in the toplevel window. I'm using win NT and python2.1.

You need to keep the image until the window is destroyed.  To deal with
the simple problem now, change the function to:

# first create a variable set to None, we'll populate it only once
_newwin_img = None
def newwin():
    global _newwin_img            # not a local variable to the function
    wat=Toplevel()
    fr=Frame(wat)
    t=Label(fr, text='Versie 1.0', fg='red', bg='white')
    t.pack(pady=10)
    filename='PythonPoweredSmall.gif'
    if not _newwin_img:                               # if not populated
      _newwin_img = PhotoImage(file=filename)
    hm = Label(fr, image=_newwin_img)
    hm.pack()
    wat.title('info')
    fr.pack()

Because of the interactions between Python and Tcl for Tkinter, the
image will be destroyed when the Python reference is destroy, which is
the end of the function.  [Most people talk about "scope" in programming,
and do not realize that "extent" is often a more important concept.]

  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |