使用 uic 将 .ui 文件转换为 Python 中的 .py 文件时出错

Error using uic to convert .ui file to .py file in Python

我正在尝试在 python 中编写一个程序,它将同一文件夹(在 Qt Designer 中创建)中的 .ui 文件转换为 .py 文件。这是这个极其基本的程序的代码:

# -*- coding: utf-8 -*-

from PyQt4 import uic

with open('exampleinterface.py', 'w') as fout:
    uic.compileUi('exampleinterface.ui', fout)

它给出了以下错误(缩短了长路径名):

Traceback (most recent call last):

File "", line 1, in

File "...\Python32_3.5\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile execfile(filename, namespace)

File "...\Python32_3.5\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 88, in execfile exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace) File ".../Documents/Python/UiToPy/minimalconvert.py", line 11, in uic.compileUi('exampleinterface.ui', fout)

File "...\Python32_3.5\lib\site-packages\PyQt4\uic__init__.py", line 173, in compileUi winfo = compiler.UICompiler().compileUi(uifile, pyfile, from_imports, resource_suffix)

File "...\Python32_3.5\lib\site-packages\PyQt4\uic\Compiler\compiler.py", line 140, in compileUi w = self.parse(input_stream, resource_suffix)

File "...\Python32_3.5\lib\site-packages\PyQt4\uic\uiparser.py", line 974, in parse document = parse(filename)

File "...\Python32_3.5\lib\xml\etree\ElementTree.py", line 1182, in parse tree.parse(source, parser)

File "...\Python32_3.5\lib\xml\etree\ElementTree.py", line 594, in parse self._root = parser._parse_whole(source) xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 1

谁能告诉我为什么这不起作用,是否有解决方案?

注意:我知道还有其他方法可以将 .ui 文件转换为 .py 文件,但我正在寻找一种可以轻松实现的方法集成到 python 程序中,无需调用外部文件。

感谢 ekhumoro 和 mwormser。问题确实是 .ui 文件。 我用一个新的 .ui 文件重试了它,一切正常。

出现此错误是因为我的 .ui 文件没有保存我最近所做的更改。文件名在文件名中显示asterisk(*)标记。一旦我保存了更改的文件,它就可以转换成 .py 文件。