开发者

How to make "gtk.gdk.pixbuf_new_from_file()" use a relative path?

开发者 https://www.devze.com 2023-01-27 07:05 出处:网络
In gtk.gdk.pixbuf_new_from_file(image), if \"image\" is not an absolute path, I get: glib.GError: Failed to open file \'image.pn开发者_如何学编程g\': No such file or directory

In gtk.gdk.pixbuf_new_from_file(image), if "image" is not an absolute path, I get:

glib.GError: Failed to open file 'image.pn开发者_如何学编程g': No such file or directory

That's unless I'm in the directory where the image lives.

note1: I'm running Debian Squeeze, which uses Python 2.6 by default.

note2: The image and the script live in one directory.


This is not specific to pixbuf_new_from_file, relative paths are always resolved relative to the current directory, which initially is the directory the script is started from.

You can get the directory the script is actually in with os.path.dirname(__file__). Then you can either make it your script's current directory with os.chdir, or you can use os.path.join to combine it with image.png.

0

精彩评论

暂无评论...
验证码 换一张
取 消