【发布时间】:2016-11-16 01:03:46
【问题描述】:
我正在尝试用 python 编写一个程序,它将同一文件夹中的 .ui 文件(在 Qt Designer 中创建)转换为 .py 文件。这是这个非常基本的程序的代码:
# -*- coding: utf-8 -*-
from PyQt4 import uic
with open('exampleinterface.py', 'w') as fout:
uic.compileUi('exampleinterface.ui', fout)
它给出了以下错误(缩短了长路径名):
Traceback(最近一次调用最后一次):
文件“”,第 1 行,在
文件“...\Python32_3.5\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py”,第 699 行,在运行文件中 execfile(文件名,命名空间)
文件“...\Python32_3.5\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py”,第 88 行,在 execfile exec(编译(打开(文件名,'rb').read(),文件名,'exec'),命名空间) 文件“.../Documents/Python/UiToPy/minimalconvert.py”,第 11 行,在 uic.compileUi('exampleinterface.ui', fout)
文件“...\Python32_3.5\lib\site-packages\PyQt4\uic__init__.py”,第 173 行,在 compileUi winfo = compiler.UICompiler().compileUi(uifile, pyfile, from_imports, resource_suffix)
文件“...\Python32_3.5\lib\site-packages\PyQt4\uic\Compiler\compiler.py”,第 140 行,在 compileUi w = self.parse(input_stream, resource_suffix)
文件“...\Python32_3.5\lib\site-packages\PyQt4\uic\uiparser.py”,第 974 行,解析中 文档 = 解析(文件名)
文件“...\Python32_3.5\lib\xml\etree\ElementTree.py”,第 1182 行,解析中 tree.parse(source, parser)
文件“...\Python32_3.5\lib\xml\etree\ElementTree.py”,第 594 行,解析中 self._root = parser._parse_whole(源) xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 1
谁能告诉我为什么这不起作用,以及是否有解决方案?
注意:我知道还有其他方法可以将 .ui 文件转换为 .py 文件,但我正在寻找一种可以轻松集成到 python 程序中的方法,而无需调用外部文件。
【问题讨论】:
-
对我来说很好。显然你的
ui文件有问题 - 但由于你没有提供它,所以没有什么可以说的。 -
您应该检查是否可以在命令行中使用
pyuic4以常规方式转换相同的文件。如果它在那里也不起作用,那么您的 .ui 文件就是问题所在。
标签: python pyqt qt-designer