python - 使用 appJar 在 TreeView 中查看 XML 文件

标签 python python-3.x tkinter

我正在使用 appJar(python 库,使用 Tkinter)制作 GUI。我有一个 XML 文件。 我正在使用 ElementTree XML 解析库解析 XML 文件。

我想在 TreeView 中查看我的 XML 文件。 所以我正在使用 Element Tree 解析文件,获取需要在 Treeview 中显示的标签并形成一个新的 XML 对象。并在 appJar 函数中传递新对象:.addTree()。 但我得到的错误是:

..lib\site-packages\appJar\appjar.py", line 8764, in addTree
    xmlDoc = parseString(data).

...lib\xml\dom\expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
TypeError: a bytes-like object is required, not 'ElementTree'

xml=et.Element(root)
print(xml)
for ele in valList:
    reg=et.SubElement(xml, ele.find('Name').text)
    bitroot= ele.findall('Bit')
    for bit in bitroot:
        et.SubElement(reg, bit.find('Name').text)

xmltree= et.ElementTree(xml)
app.startFrame('bottomleft',1,0,2)
app.setBg('orange')
app.setSticky('news')
app.setStretch('none')
app.addTree('REGISTER', xmltree)

I am getting the error, as far as I can understand is because .addTree() API is unable to read the format of xmltree variable.

最佳答案

根据 appJar documentation ,您需要将 XML 字符串传递给 .addTree(),而不是 ElementTree。根据ElementTree documentation ,您可以使用 xml.etree.ElementTree.tostring() 从您的 Element 构建 XML 字符串:

xml_string = et.tostring(xml)
app.addTree('REGISTER', xml_string)

关于python - 使用 appJar 在 TreeView 中查看 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50446859/

相关文章:

python - 如何在 Tensorflow 训练期间将 [3751,4] 数据集密集并 reshape 为 [1,6] 数据集

python - JavaScript 的 Array.prototype.find 的 python 等价物是什么?

python - 什么?在 python pyodbc 模块中意味着

python - 无法识别凝聚聚类的新 distance_threshold 参数

python - "from __future__ import annotations"在 VSCode 中产生 "annotations is not defined"

当控制台具有焦点时,Python 使用 Tkinter 捕获事件

python - 如何使用 pandas 填充缺失的时间数据

django - 如何在 Django 单元测试中验证我的上下文是否包含表单对象?

python - Tkinter 中的 MoviePy/ffmpeg 进度条

python - 如何使用 tkinter 返回 Base 64 编码图形的高度和宽度