55"""
66import contextlib
77import os
8+ import sys
9+ from tempfile import gettempdir
810import tkinter as tk
911from 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 :
0 commit comments