从Tcl字典中获取Python字典

2024-10-02 06:20:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从Tcl字典中得到一个python字典tk.呼叫. 这有可能吗?你知道吗

示例:

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, "...")得到每个值,但是我想用一个tk.call得到Python字典(参见desiredDict)。这不是gui的问题,我不是在用gui工作。你知道吗


Tags: dataget字典footkintercalltcldict
2条回答

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

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

Tkinter中没有用于检索Tcl dict的公共函数,但有一个私有函数:

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

相关问题 更多 >

    热门问题