Skip to content

Commit cffd1a4

Browse files
committed
Create overriding loaders.cache on win32 platform
If loaders.cache specifies absolute paths to the GDK Pixbuf loader DLLs, the loader DLLs are always found regardless of their otherwise relative location to the name returned by win32 function GetModuleFileName
1 parent 3c33675 commit cffd1a4

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

gttk/__init__.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"""
66
import contextlib
77
import os
8+
import sys
9+
from tempfile import gettempdir
810
import tkinter as tk
911
from typing import Optional, Tuple
1012

@@ -26,7 +28,8 @@ class GTTK(object):
2628

2729
FOLDER = os.path.abspath(os.path.dirname(__file__))
2830

29-
def __init__(self, window: tk.Tk, theme: Optional[str] = None, theme_dir_prefix: Optional[str] = None):
31+
def __init__(self, window: tk.Tk, theme: Optional[str] = None, theme_dir_prefix: Optional[str] = None,
32+
temp_dir: str = gettempdir()):
3033
"""
3134
Initialize gttk and load it into a window
3235
@@ -36,14 +39,31 @@ def __init__(self, window: tk.Tk, theme: Optional[str] = None, theme_dir_prefix:
3639
given, defaults to the current working directory. If this
3740
has the special value "LIB", the script will use the
3841
site-packages directory where it is installed.
42+
:param temp_dir: Absolute path to temporary files directory that
43+
may be used by the library
3944
"""
4045
self.tk = window.tk
41-
folder = os.path.dirname(os.path.abspath(__file__)).replace("\\", "/")
46+
folder = os.path.dirname(os.path.abspath(__file__))
47+
4248
if theme_dir_prefix is not None:
4349
os.environ["GTK_DATA_PREFIX"] = theme_dir_prefix
4450

51+
# Create loaders.cache on win32 platforms to find pixbuf loaders properly
52+
if "win" in sys.platform:
53+
target = os.path.join(temp_dir, "loaders.cache")
54+
with open(os.path.join(folder, "loaders.cache")) as fi, open(target, "w") as fo:
55+
cache = fi.read()
56+
abspath = (os.path.join(folder, "lib", "gdk-pixbuf-2.0", "2.10.0", "loaders") + "\\")\
57+
.replace("\\", "\\\\") # loaders.cache uses double \ everywhere
58+
cache_w_abspaths = cache.replace("\\\\lib", abspath)
59+
fo.write(cache_w_abspaths)
60+
# Set GDK_PIXBUF_MODULE_FILE to the path of the new cache file
61+
# GDK_PIXBUF_MODULEDIR does not do anything for plain GDK!
62+
os.environ["GDK_PIXBUF_MODULE_FILE"] = target
63+
4564
with chdir(folder):
46-
self.tk.eval("set dir {0}; source {0}/pkgIndex.tcl".format(folder))
65+
# Evaluate pkgIndex.tcl, Tcl does not handle \ as a pathsep, so with /
66+
self.tk.eval("set dir {0}; source {0}/pkgIndex.tcl".format(folder.replace("\\", "/")))
4767
self.tk.call("package", "require", "ttk::theme::gttk")
4868

4969
if theme is not None:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def copy_to_target(self, target: str):
159159
specials={
160160
"libpixmap.dll": "/lib/gtk-2.0/2.10.0/engines/",
161161
"libwimp.dll": "/lib/gtk-2.0/2.10.0/engines/",
162-
"loaders.cache": "/lib/gdk-pixbuf-2.0/2.10.0/"}
162+
"loaders.cache": "/"} # loaders.cache is used to specify abspaths to the loaders
163163
specials.update({"libpixbufloader-{}.dll".format(fmt): "/lib/gdk-pixbuf-2.0/2.10.0/loaders/"
164164
for fmt in ["ani", "bmp", "gif", "icns", "ico", "jpeg", "png", "pnm", "qtif", "svg", "tga", "tiff", "xbm", "xpm"]})
165165
DependencyWalker("libgttk.dll", specials=specials).copy_to_target("gttk")

0 commit comments

Comments
 (0)