Skip to content

Commit fdc6e79

Browse files
authored
Some improvements around cget/configure (#8)
1 parent 777cfbb commit fdc6e79

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

tksvg/__init__.py

+25-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import tkinter as tk
77
import os
88

9+
910
def load(window: tk.Tk):
1011
"""Load tksvg into a Tk interpreter"""
1112
local = os.path.abspath(os.path.dirname(__file__))
@@ -26,39 +27,50 @@ class SvgImage(tk.PhotoImage):
2627
This implementation is inspired by GitHub @j4321:
2728
<https://stackoverflow.com/a/64829808>
2829
"""
29-
_svg_options = [("scale", float), ("scaletowidth", int), ("scaletoheight", int)]
30+
__svg_options = {"scale": float, "scaletowidth": int, "scaletoheight": int}
3031

3132
def __init__(self, name=None, cnf={}, master=None, **kwargs):
32-
self._svg_options_current = dict()
3333
# Load TkSVG package if not yet loaded
3434
master = master or tk._default_root
3535
if master is None:
3636
raise tk.TclError("No Tk instance available to get interpreter from")
3737
if not getattr(master, "_tksvg_loaded", False):
3838
load(master)
39+
3940
# 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+
4243
tk.PhotoImage.__init__(self, name, cnf, master, **kwargs)
4344
self.configure(**svg_options)
4445

4546
def configure(self, **kwargs):
4647
"""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:
4950
tk.PhotoImage.configure(self, **kwargs)
50-
options = tuple()
51+
52+
options = ""
5153
for key, value in svg_options.items():
5254
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
5660

5761
def cget(self, option):
5862
"""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
6274

6375
def __getitem__(self, key):
6476
return self.cget(key)

0 commit comments

Comments
 (0)