Discussion:
Tkinter PhotoImage, no such file or directory
Patrick Kramer
2006-04-28 07:30:19 UTC
Permalink
I have two files which are both in the PyGlucoCE directory off of root.

Tkintertest.py
BtnGluco.gif



Here is my code:

import sys

sys.path.append('\\Program Files\\Python24\\python24.zip\\lib-tk')
sys.path.append('\\PyGlucoCE')

from Tkinter import *

root = Tk()

ImgGlucose = PhotoImage(file = 'BtnGluco.gif')

print ImgGlucose.height()

b = Button(root, image=ImgGlucose)
b.image = ImgGlucose
b.pack()

root.mainloop()



Now I get this error:

_tkinter.TclError: couldn't open "BtnGluco.gif": no such file or directory




The weird part is I get the same error within Pydev, but if I run the script straight through commandline, it works and shows the image as a button.

My guess is I screwed up the path append, but it looks correct to me :/
Luke Dunstan
2006-04-28 10:58:12 UTC
Permalink
----- Original Message -----
From: "Patrick Kramer" <***@meton.net>
To: <***@python.org>
Sent: Friday, April 28, 2006 3:30 PM
Subject: [PythonCE] Tkinter PhotoImage, no such file or directory
Post by Patrick Kramer
I have two files which are both in the PyGlucoCE directory off of root.
Tkintertest.py
BtnGluco.gif
import sys
sys.path.append('\\Program Files\\Python24\\python24.zip\\lib-tk')
This looks OK.
Post by Patrick Kramer
sys.path.append('\\PyGlucoCE')
What is the purpose of the above line? I don't think you are importing any
modules from that directory.
Post by Patrick Kramer
from Tkinter import *
root = Tk()
ImgGlucose = PhotoImage(file = 'BtnGluco.gif')
I think that should be: ImgGlucose = PhotoImage(file =
'\\PyGlucoCE\\BtnGluco.gif')

On the other hand, I don't know Tkinter so I may be wrong. Does Tkinter
search sys.path for image files?
Post by Patrick Kramer
print ImgGlucose.height()
b = Button(root, image=ImgGlucose)
b.image = ImgGlucose
b.pack()
root.mainloop()
_tkinter.TclError: couldn't open "BtnGluco.gif": no such file or directory
The weird part is I get the same error within Pydev, but if I run the
script straight through commandline, it works and shows the image as a
button.
What is Pydev, and which command line are you talking about? Windows CE or
Windows XP?

Luke
Post by Patrick Kramer
My guess is I screwed up the path append, but it looks correct to me :/
Michael Foord
2006-04-28 12:35:36 UTC
Permalink
Post by Patrick Kramer
I have two files which are both in the PyGlucoCE directory off of root.
Tkintertest.py
BtnGluco.gif
import sys
sys.path.append('\\Program Files\\Python24\\python24.zip\\lib-tk')
sys.path.append('\\PyGlucoCE')
This appends the PyGluoCE directory to sys.path.
Post by Patrick Kramer
from Tkinter import *
root = Tk()
ImgGlucose = PhotoImage(file = 'BtnGluco.gif')
This tells PhotoImage to fetch the file 'BtnGluco.gif' from the current
directory. This is your problem I believe. Pass an absolute path here
and it should go away.
Post by Patrick Kramer
print ImgGlucose.height()
b = Button(root, image=ImgGlucose)
b.image = ImgGlucose
b.pack()
root.mainloop()
_tkinter.TclError: couldn't open "BtnGluco.gif": no such file or directory
The weird part is I get the same error within Pydev, but if I run the script straight through commandline, it works and shows the image as a button.
Probably because you run the script from inside that directory on your
desktop.

PythonCE starts with the current directory somewhere else.
Post by Patrick Kramer
My guess is I screwed up the path append, but it looks correct to me :/
I'm pretty sure that Tkinter *doesn't* search sys.path for image files.

HTH

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
Post by Patrick Kramer
_______________________________________________
PythonCE mailing list
http://mail.python.org/mailman/listinfo/pythonce
Patrick Kramer
2006-04-29 02:28:16 UTC
Permalink
<quote who="Michael Foord">
Post by Michael Foord
Post by Patrick Kramer
I have two files which are both in the PyGlucoCE directory off of root.
Tkintertest.py
BtnGluco.gif
import sys
sys.path.append('\\Program Files\\Python24\\python24.zip\\lib-tk')
sys.path.append('\\PyGlucoCE')
This appends the PyGluoCE directory to sys.path.
Post by Patrick Kramer
from Tkinter import *
root = Tk()
ImgGlucose = PhotoImage(file = 'BtnGluco.gif')
This tells PhotoImage to fetch the file 'BtnGluco.gif' from the current
directory. This is your problem I believe. Pass an absolute path here
and it should go away.
Post by Patrick Kramer
print ImgGlucose.height()
b = Button(root, image=ImgGlucose)
b.image = ImgGlucose
b.pack()
root.mainloop()
_tkinter.TclError: couldn't open "BtnGluco.gif": no such file or directory
The weird part is I get the same error within Pydev, but if I run the script straight through commandline, it works and shows the image as a
button.
Probably because you run the script from inside that directory on your
desktop.
PythonCE starts with the current directory somewhere else.
Post by Patrick Kramer
My guess is I screwed up the path append, but it looks correct to me :/
I'm pretty sure that Tkinter *doesn't* search sys.path for image files.
HTH
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
That worked, thanks... So is there a way to tell pythonce were the current directory is?

Another problem, which I think I read about earlier, is how photoimage screws up how images are displayed on windows CE. Is there a workaround for
this, or am I doing something wrong.
Luke Dunstan
2006-04-29 08:19:15 UTC
Permalink
----- Original Message -----
From: "Patrick Kramer" <***@meton.net>
To: <***@python.org>
Sent: Saturday, April 29, 2006 10:28 AM
Subject: Re: [PythonCE] Tkinter PhotoImage, no such file or directory
Post by Patrick Kramer
That worked, thanks... So is there a way to tell pythonce were the current directory is?
You can tell Python using os.chdir(), but that will not necessarily have any
effect on Tkinter because the operating system has no concept of a "current
directory", so it depends on whether the image file is opened in the Python
code or the C code.

Luke
Patrick Kramer
2006-04-30 09:25:53 UTC
Permalink
<quote who="Luke Dunstan">
Post by Luke Dunstan
You can tell Python using os.chdir(), but that will not necessarily have any
effect on Tkinter because the operating system has no concept of a "current
directory", so it depends on whether the image file is opened in the Python
code or the C code.
Luke
So there is no way for python to return the current directory it is in?

say something like:

dir = return_dir()

image_loc = dir + "//BtnGluco.gif"

Or:

image_loc = dir + "//assets//img//BtnGluco.gif" #This is how I would like to orgnize it
Michael Foord
2006-04-30 09:57:33 UTC
Permalink
Post by Patrick Kramer
<quote who="Luke Dunstan">
Post by Luke Dunstan
You can tell Python using os.chdir(), but that will not necessarily have any
effect on Tkinter because the operating system has no concept of a "current
directory", so it depends on whether the image file is opened in the Python
code or the C code.
Luke
So there is no way for python to return the current directory it is in?
dir = return_dir()
image_loc = dir + "//BtnGluco.gif"
image_loc = dir + "//assets//img//BtnGluco.gif" #This is how I would like to orgnize it
I think the situation is (perhaps Luke can correct me if I'm wrong) :

The underlying Windows CE platform has no concept of a current directory.
PythonCE *simulates* a current directory on the python level

This means that calls that use only Python code will work as normal -
except the current directory will always start in a fixed location
('//Temp' I think).
Calls to use files from C code will not have access to this information.

So you can experiment with the image_loc and see if it works using the
current directory or not, but it may not do.

To get the current directory you use the normal functions provided by
the ``os`` and ``os.path`` module :

cur_dir = os.getcwd()
print cur_dir
os.chdir(""//assets")
print os.getcwd()

image_path = os.path.join(os.getcwd(), "img//BtnGluco.gif")

You can also test whether you are running on windows CE or not with either :

sys.platform or sys.getwindowsversion()

They should return different things on windows CE and normal windows.
That means you can make your code behave appropriately depending on
which system it is on.

HTH

Fuzzyman
http://www.voidspace.org.uk/python/shareware.shtml
Post by Patrick Kramer
_______________________________________________
PythonCE mailing list
http://mail.python.org/mailman/listinfo/pythonce
Luke Dunstan
2006-04-30 16:20:44 UTC
Permalink
----- Original Message -----
From: "Michael Foord" <***@voidspace.org.uk>
To: "Patrick Kramer" <***@meton.net>
Cc: <***@python.org>
Sent: Sunday, April 30, 2006 5:57 PM
Subject: Re: [PythonCE] Tkinter PhotoImage, no such file or directory
Post by Michael Foord
Post by Patrick Kramer
<quote who="Luke Dunstan">
Post by Luke Dunstan
You can tell Python using os.chdir(), but that will not necessarily have any
effect on Tkinter because the operating system has no concept of a "current
directory", so it depends on whether the image file is opened in the Python
code or the C code.
Luke
So there is no way for python to return the current directory it is in?
dir = return_dir()
image_loc = dir + "//BtnGluco.gif"
image_loc = dir + "//assets//img//BtnGluco.gif" #This is how I would like to orgnize it
The underlying Windows CE platform has no concept of a current directory.
PythonCE *simulates* a current directory on the python level
Correct.
Post by Michael Foord
This means that calls that use only Python code will work as normal -
except the current directory will always start in a fixed location
('//Temp' I think).
That is true at the moment, but in the next release I will change it so that
if you run a script using the command line then the current directory will
initially be set to the directory containing the script, for convenience.
Post by Michael Foord
Calls to use files from C code will not have access to this information.
So you can experiment with the image_loc and see if it works using the
current directory or not, but it may not do.
To get the current directory you use the normal functions provided by
cur_dir = os.getcwd()
print cur_dir
os.chdir(""//assets")
print os.getcwd()
Correct, except that the path separator is \ (or \\ in a string literal) not
//.
Post by Michael Foord
image_path = os.path.join(os.getcwd(), "img//BtnGluco.gif")
or continuing with that pattern:

image_path = os.path.join(os.getcwd(), "img", "BtnGluco.gif")
Post by Michael Foord
sys.platform or sys.getwindowsversion()
They should return different things on windows CE and normal windows.
That means you can make your code behave appropriately depending on
which system it is on.
HTH
Fuzzyman
http://www.voidspace.org.uk/python/shareware.shtml
That will work, but like Gonzalo I prefer using os.name.

Luke

Gonzalo Monzón
2006-04-30 09:40:11 UTC
Permalink
Post by Patrick Kramer
<quote who="Luke Dunstan">
Post by Luke Dunstan
You can tell Python using os.chdir(), but that will not necessarily have any
effect on Tkinter because the operating system has no concept of a "current
directory", so it depends on whether the image file is opened in the Python
code or the C code.
Luke
So there is no way for python to return the current directory it is in?
dir = return_dir()
image_loc = dir + "//BtnGluco.gif"
image_loc = dir + "//assets//img//BtnGluco.gif" #This is how I would like to orgnize it
_______________________________________________
PythonCE mailing list
http://mail.python.org/mailman/listinfo/pythonce
Hi,

I use this:

if (OS == "ce"):
path = sys.path[0]
else:
path = os.curdir


It works right for me.

Gonzalo Monzón.
Loading...