By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,985 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,985 IT Pros & Developers. It's quick & easy.

Insert image to a List box

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.