Ocaso Protal
Ocaso Protal

Reputation: 20257

Get Python dictionary from Tcl dictionary

I want to get a python dictionary from a Tcl dictionary in a single tk.call. Is this somehow possible?

Example:

import tkinter

desiredDict =  {"first": "Foo", "second": "Bar", "third": "Baz"}

tk = tkinter.Tcl()
tk.call("set", "data(first)", "Foo" )
tk.call("set", "data(second)", "Bar" )
tk.call("set", "data(third)", "Baz" )
foo = tk.call("array", "get", "data" )
tclKeys =  tk.call("dict", "keys", foo)
fromTcl  = tk.call("dict", "get", foo, "first")

print(foo)
print(tclKeys)
print(fromTcl)
print(type(foo))
# print(dir(foo))

I know I can get the keys with tk.call("dict", "keys", foo) and then every single value with tk.call("dict", "get", foo, "...") but I want to get the Python dictionary (see desiredDict) in one single tk.call. This is no gui problem, I'm not working wit a gui here.

Upvotes: 1

Views: 899

Answers (2)

jasonharper
jasonharper

Reputation: 9597

There's no public function in Tkinter for retrieving a Tcl dict, but there is a private one:

>>> tkinter._splitdict(tk, foo)
{'second': 'Bar', 'third': 'Baz', 'first': 'Foo'}

Upvotes: 2

fbmch
fbmch

Reputation: 721

I don't know where the relevant documentation is but if you wanted to go with Python and the methods you presented, this does work:

keys = desiredDict.keys()
d = dict(zip(keys, (tk.call("dict", "get", foo, key) for key in keys)))
assert d == desiredDict

Upvotes: 1

Related Questions