Tkinter PhotoImage won't show ;-(

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vm

    #1

    Tkinter PhotoImage won't show ;-(

    please help!

    I can't find anything wrong (except the result ofc ;-)

    This:



    picfile = 'logo.gif'

    pic = tk.PhotoImage(f ile=picfile, master=root)

    canpic = cv.create_image (320, 240, image=pic) #cv is is a Tkinter
    Canvas ofc



    ....works just fine, but when I put it in the event binding function, it
    doesn't work,

    it doesn't report any errors too, I'm sure the function went all the way
    through

    because I print the test-text at the end of the function... But it doesn't
    show picture!



    here's the entire program I wrote to test this out:

    =============== =============== =============== ======

    import Tkinter as tk, os



    root = tk.Tk()

    root.title = 'Picviewer'



    ###setting Listbox, Canvas

    lb = tk.Listbox(root ,width =20, height=22)

    lb.grid(column= 0, row=0, columnspan = 2)



    cv = tk.Canvas(root, width=640, height=480, bg='white')

    cv.grid(column= 2,row=0, rowspan=2)



    ###listing all .gifs in current working directory into the Listbox lb

    cwd = os.getcwd()

    filelista = os.listdir(cwd)

    filelista.sort( )

    for element in filelista:

    if os.path.isfile( element) and '.gif' in element.lower() : lb.insert(
    lb.size() ,element)



    ###Load-button bind function (showing picture) - the problem seems to be in
    here somewhere I think

    def putpic(event):

    picfile = lb.selection_ge t()

    pic = tk.PhotoImage(f ile=picfile, master=root)

    canpic = cv.create_image (320, 240, image=pic)

    ###just to see what's going on

    print str(picfile), "\n", str(pic), '\n', str(canpic)



    ###setting Load-button

    load = tk.Button(root, text='Load', width=16)

    load.bind('<But ton-1>', putpic)

    load.grid(colum n=0, row=2, columnspan=2)



    ###Putting the logo image at start (more like test image at start - and it
    works)

    pic = tk.PhotoImage(f ile='logo.gif', master=root)

    canpic = cv.create_image (320, 240, image=pic)



    root.mainloop()

    =============== =============== =============== ===

    Maybe I just need a fresh pair of eyes, SOMEONE!




  • Fredrik Lundh

    #2
    Re: Tkinter PhotoImage won't show ;-(

    "vm" wrote:
    [color=blue]
    > I can't find anything wrong (except the result ofc ;-)
    >
    > This:
    >
    > picfile = 'logo.gif'
    >
    > pic = tk.PhotoImage(f ile=picfile, master=root)
    >
    > canpic = cv.create_image (320, 240, image=pic) #cv is is a Tkinter
    > Canvas ofc
    >
    > ...works just fine, but when I put it in the event binding function, it
    > doesn't work,[/color]

    this is explained in the python faq, and towards the end of this
    page:



    </F>



    Comment

    • vm

      #3
      Re: Tkinter PhotoImage won't show ***SOLVED***

      I was missing "global pic" line at the begining of
      putpic function. doh!


      "vm" <not.my@email.c om> wrote in message news:driptr$jih $1@ss405.t-com.hr...[color=blue]
      > please help!
      >
      > I can't find anything wrong (except the result ofc ;-)
      >
      > This:
      >
      >
      >
      > picfile = 'logo.gif'
      >
      > pic = tk.PhotoImage(f ile=picfile, master=root)
      >
      > canpic = cv.create_image (320, 240, image=pic) #cv is is a Tkinter
      > Canvas ofc
      >
      >
      >
      > ...works just fine, but when I put it in the event binding function, it
      > doesn't work,
      >
      > it doesn't report any errors too, I'm sure the function went all the way
      > through
      >
      > because I print the test-text at the end of the function... But it doesn't
      > show picture!
      >
      >
      >
      > here's the entire program I wrote to test this out:
      >
      > =============== =============== =============== ======
      >
      > import Tkinter as tk, os
      >
      >
      >
      > root = tk.Tk()
      >
      > root.title = 'Picviewer'
      >
      >
      >
      > ###setting Listbox, Canvas
      >
      > lb = tk.Listbox(root ,width =20, height=22)
      >
      > lb.grid(column= 0, row=0, columnspan = 2)
      >
      >
      >
      > cv = tk.Canvas(root, width=640, height=480, bg='white')
      >
      > cv.grid(column= 2,row=0, rowspan=2)
      >
      >
      >
      > ###listing all .gifs in current working directory into the Listbox lb
      >
      > cwd = os.getcwd()
      >
      > filelista = os.listdir(cwd)
      >
      > filelista.sort( )
      >
      > for element in filelista:
      >
      > if os.path.isfile( element) and '.gif' in element.lower() :
      > lb.insert( lb.size() ,element)
      >
      >
      >
      > ###Load-button bind function (showing picture) - the problem seems to be
      > in here somewhere I think
      >
      > def putpic(event):
      >
      > picfile = lb.selection_ge t()
      >
      > pic = tk.PhotoImage(f ile=picfile, master=root)
      >
      > canpic = cv.create_image (320, 240, image=pic)
      >
      > ###just to see what's going on
      >
      > print str(picfile), "\n", str(pic), '\n', str(canpic)
      >
      >
      >
      > ###setting Load-button
      >
      > load = tk.Button(root, text='Load', width=16)
      >
      > load.bind('<But ton-1>', putpic)
      >
      > load.grid(colum n=0, row=2, columnspan=2)
      >
      >
      >
      > ###Putting the logo image at start (more like test image at start - and it
      > works)
      >
      > pic = tk.PhotoImage(f ile='logo.gif', master=root)
      >
      > canpic = cv.create_image (320, 240, image=pic)
      >
      >
      >
      > root.mainloop()
      >
      > =============== =============== =============== ===
      >
      > Maybe I just need a fresh pair of eyes, SOMEONE!
      >
      >
      >
      >[/color]


      Comment

      Working...