471,330 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Insert image to a List box

I run the following code and got the error (I put a .gif file on the desktop)
Traceback (most recent call last):
File "11.py", line 25, in <module>
for gifname in os.listdir(dirpath):
OSError: [Errno 2] No such file or directory: '.\\Desktop\\'

import os
import Tkinter

root = Tkinter.Tk()
L = Tkinter.Listbox(selectmode=Tkinter.SINGLE)
gifsdict = {}

dirpath = '.\\Desktop\\'
for gifname in os.listdir(dirpath):
if not gifname[0].isdigit():
continue
gifpath = os.path.join(dirpath, gifname)
gif = Tkinter.PhotoImage(file=gifpath)
gifsdict[gifname] = gif
L.insert(Tkinter.END, gifname)

L.pack()
img = Tkinter.Label()
img.pack()
def list_entry_clicked(*ignore):
imgname = L.get(L.curselection()[0])
img.config(image=gifsdict[imgname])
L.bind('<ButtonRelease-1>', list_entry_clicked)
root.mainloop()
Nov 15 '07 #1
1 5366
Thanks a lot for your patience.
I put the gif file to a folder called fig in my desktop.
dirpath = './fig'
still got error:

Traceback (most recent call last):
File "11.py", line 238, in <module>
img.config(image=gifsdict[imgname])
NameError: name 'imgname' is not defined
You should reply to the python list. That way everyone gets the
benefit of your questions.

The problem is here:
Expand|Select|Wrap|Line Numbers
  1. def list_entry_clicked(*ignore):
  2. imgname = L.get(L.curselection()[0])
  3. img.config(image=gifsdict[imgname])
  4. L.bind('<ButtonRelease-1>', list_entry_clicked)
  5.  
I think the main problem is that the line
`img.config(image=gifsdict[imgname])' is supposed to be part of
`list_entry_clicked' and it isn't.

What you want to do (maybe? I made some assumptions):
Expand|Select|Wrap|Line Numbers
  1. def list_entry_clicked(*ignore):
  2. imgname = L.get(L.curselection()[0])
  3. img.config(image=gifsdict[imgname])
  4.  
  5. L.bind('<ButtonRelease-1>', list_entry_clicked)
  6.  
That _should_ work, but it is untested.

Matt
Nov 16 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by EricRobineau | last post: by
2 posts views Thread by Geoffrey KRETZ | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.