从 Tcl 词典中获取 Python 词典

Get Python dictionary from Tcl dictionary

我想在单个 tk.call 中从 Tcl 字典中获取 python 字典。这有可能吗?

示例:

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))

我知道我可以使用 tk.call("dict", "keys", foo) 获取键,然后使用 tk.call("dict", "get", foo, "...") 获取每个值,但我想获取 Python 字典(参见 desiredDict)一首单曲 tk.call。这不是 gui 问题,我在这里没有使用 gui。

我不知道相关文档在哪里,但如果您想使用 Python 和您提供的方法,这确实有效:

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

Tkinter 中没有 public 函数来检索 Tcl 字典,但有一个私有函数:

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