6
6
import tkinter as tk
7
7
import os
8
8
9
+
9
10
def load (window : tk .Tk ):
10
11
"""Load tksvg into a Tk interpreter"""
11
12
local = os .path .abspath (os .path .dirname (__file__ ))
@@ -26,39 +27,50 @@ class SvgImage(tk.PhotoImage):
26
27
This implementation is inspired by GitHub @j4321:
27
28
<https://stackoverflow.com/a/64829808>
28
29
"""
29
- _svg_options = [( "scale" , float ), ( "scaletowidth" , int ), ( "scaletoheight" , int )]
30
+ __svg_options = { "scale" : float , "scaletowidth" : int , "scaletoheight" : int }
30
31
31
32
def __init__ (self , name = None , cnf = {}, master = None , ** kwargs ):
32
- self ._svg_options_current = dict ()
33
33
# Load TkSVG package if not yet loaded
34
34
master = master or tk ._default_root
35
35
if master is None :
36
36
raise tk .TclError ("No Tk instance available to get interpreter from" )
37
37
if not getattr (master , "_tksvg_loaded" , False ):
38
38
load (master )
39
+
39
40
# Pop SvgImage keyword arguments
40
- svg_options = {key : t ( kwargs .pop (key )) for ( key , t ) in self ._svg_options if key in kwargs }
41
- # Initialize as a PhotoImage
41
+ svg_options = {key : kwargs .pop (key ) for key in self .__svg_options if key in kwargs }
42
+
42
43
tk .PhotoImage .__init__ (self , name , cnf , master , ** kwargs )
43
44
self .configure (** svg_options )
44
45
45
46
def configure (self , ** kwargs ):
46
47
"""Configure the image with SVG options and pass to PhotoImage.configure"""
47
- svg_options = {key : t ( kwargs .pop (key )) for ( key , t ) in self ._svg_options if key in kwargs }
48
- if kwargs : # len(kwargs) > 0
48
+ svg_options = {key : kwargs .pop (key ) for key in self .__svg_options if key in kwargs }
49
+ if kwargs :
49
50
tk .PhotoImage .configure (self , ** kwargs )
50
- options = tuple ()
51
+
52
+ options = ""
51
53
for key , value in svg_options .items ():
52
54
if value is not None :
53
- options += ("-" + key , str (value ))
54
- self .tk .eval ("%s configure -format {svg %s}" % (self .name , " " .join (options )))
55
- self ._svg_options_current .update (svg_options )
55
+ options += f"-{ key } { value } "
56
+
57
+ self .tk .eval ("%s configure -format {svg %s}" % (self .name , options ))
58
+
59
+ config = configure
56
60
57
61
def cget (self , option ):
58
62
"""Return the option set for an SVG property or pass to PhotoImage.cget"""
59
- if option in (k for k , _ in self ._svg_options ):
60
- return self ._svg_options_current .get (option , None )
61
- return tk .PhotoImage .cget (self , option )
63
+ if option not in self .__svg_options :
64
+ return tk .PhotoImage .cget (self , option )
65
+
66
+ type = self .__svg_options [option ]
67
+ format_list = tk .PhotoImage .cget (self , "format" )
68
+
69
+ for index , item in enumerate (format_list ):
70
+ if str (item )[1 :] == option :
71
+ return type (format_list [index + 1 ])
72
+
73
+ return None
62
74
63
75
def __getitem__ (self , key ):
64
76
return self .cget (key )
0 commit comments