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

Leo*_*eon 3 python pyqt qt-designer

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

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

from PyQt4 import uic

with open('exampleinterface.py', 'w') as fout:
    uic.compileUi('exampleinterface.ui', fout)
Run Code Online (Sandbox Code Playgroud)

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

回溯(最近一次调用最后一次):

文件“”,第 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(compile(open(filename, 'rb').read(), filename , '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 行,在解析文档 = parse(filename) 中

文件“...\Python32_3.5\lib\xml\etree\ElementTree.py”,第 1182 行,在 parse tree.parse(source, parser) 中

文件“...\Python32_3.5\lib\xml\etree\ElementTree.py”,第 594 行,解析 self._root = parser._parse_whole(source) xml.etree.ElementTree.ParseError:格式不正确(无效)标记):第 1 行,第 1 列

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

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

小智 5

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