Insert image to a List box

L

linda.s

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()
 
M

Matimus

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()

The exception points to this line as being the issue:
for gifname in os.listdir(dirpath):

and the error says `No such file or directory: '.\\Desktop\\''

So, there must be no directory named '.\\Desktop\\' in your current
working directory. To find out your current working directory use
`os.getcwd()'. Make sure that `os.getcwd()' returns the path to a
directory with a Desktop folder in it. This is usually something like
'C:\\Documents and Settings\\username'.

Matt

Matt
 
M

Matt McCredie

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()

The exception points to this line as being the issue:
for gifname in os.listdir(dirpath):

and the error says `No such file or directory: '.\\Desktop\\''

So, there must be no directory named '.\\Desktop\\' in your current
working directory. To find out your current working directory use
`os.getcwd()'. Make sure that `os.getcwd()' returns the path to a
directory with a Desktop folder in it. This is usually something like
'C:\\Documents and Settings\\username'.

In my mac machine,
I got something like:'/Users/linda/Desktop'

So I changed the code like:
dirpath = '.\Users\linda\Desktop\\'

But I still got the error:
ValueError: invalid \x escape

Two things. When you type out a path, if you include a '.' in the
front, that means that the path is relative to your current working
directory. So, if your current working directory is
/Users/linda/Desktop, and dirpath is set to './Users/linda/desktop',
then os.listdir(dirpath) is going to search
'/Users/linda/desktop/Users/linda/Desktop', which probably doesn't
exist. You want to use an absolute path just drop the '.'. If you want
to get the files from the current directory, just use a single '.'.
The following two things should work:

dirpath = "." # This always uses the current working directory
dirpath = "/Users/linda/Desktop" # This will always use the same directory

The second thing, which I think is the error you are currently seeing,
is the escape error. In strings in python the backslash is used as an
escape character, if you want to type a single backslash you have to
type two of them '\\' or, for paths (especially on a mac) is is
completely valid to use forward slashes instead. Also, there are raw
strings which don't do escaping. Any of the following will work:

dirpath = "\\Users\\linda\\Desktop"
dirpath = "/Users/linda/Desktop"
dirpath = r"\Users\linda\Desktop" # prepending an 'r' makes it a raw
string (no escaping)


HTH

Matt
 
M

Matt McCredie

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:
Code:
def list_entry_clicked(*ignore):
   imgname = L.get(L.curselection()[0])
img.config(image=gifsdict[imgname])
L.bind('<ButtonRelease-1>', list_entry_clicked)

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):
Code:
def list_entry_clicked(*ignore):
   imgname = L.get(L.curselection()[0])
   img.config(image=gifsdict[imgname])

L.bind('<ButtonRelease-1>', list_entry_clicked)

That _should_ work, but it is untested.

Matt
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top