Re: Tkinter Image Button Bug

Discussion in 'Python' started by Russell E. Owen, Jul 2, 2003.

  1. In article <>,
    Erhan Ekici <> wrote:

    >There is a problem in my code.I think it is Tkinter bug....
    >it works but doesn't display image. Instead of
    >image, only a square with color=#dddddd comes on screen.


    I think you are running into a common Tkinter pitfall (it is an
    intentional design decision, but I do not recall the argument for it):
    you need to explicitly maintain references to images; putting them in a
    Tkinter widget is not sufficient.

    In your code "icon" is garbage-collected when the function exits because
    using it to set the icon of the label b does not increment its reference
    count.

    I am almost certain it suffices to assign b._iconref = icon (_iconref
    being an attribute that the Label b is not likely to already have).

    I've appended your code suitably modified. I've not tested it.

    -- Russell

    >from Tkinter import *
    >
    >main=Tk()
    >
    >def toolbar(master):
    > toolbar= Frame(master)
    > pfile ="C:\\Python22\\samples\\ftp\\images\\help.gif"
    > icon = PhotoImage(file=pfile)
    > b=Label(toolbar, image=icon, width=10)

    b._iconref = icon
    > b.config(background="#dddddd", bd=1, relief=FLAT)
    > b.pack(side=LEFT, padx=2, pady=2)
    > #toolbar.config(background="#ffffff", bd=2, relief=GROOVE)
    > toolbar.pack(side=TOP, expand=YES, fill=X)
    >
    >
    >toolbar(main)
    >main.mainloop()
     
    Russell E. Owen, Jul 2, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    880
    Purvi T
    Oct 19, 2004
  2. Fredrik Lundh

    Re: Tkinter Image Button Bug

    Fredrik Lundh, Jul 2, 2003, in forum: Python
    Replies:
    0
    Views:
    385
    Fredrik Lundh
    Jul 2, 2003
  3. Elaine Jackson

    Tkinter Button image option

    Elaine Jackson, Apr 11, 2004, in forum: Python
    Replies:
    4
    Views:
    5,708
    Fredrik Lundh
    Apr 16, 2004
  4. Casey Hawthorne
    Replies:
    0
    Views:
    320
    Casey Hawthorne
    May 12, 2005
  5. iwasjoeking
    Replies:
    9
    Views:
    632
    dorayme
    Jun 10, 2008
Loading...

Share This Page