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

我试图用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程序中而无需调用外部文件的方法.

解决方法:

出现此错误是因为我的.ui文件未保存在我最近所做的更改中.文件名在文件名中显示星号(*).一旦我保存了更改文件,就可以将其转换为.py文件.

上一篇:第一次写博客


下一篇:python-matplotlib事件侦听器未在PyQT小部件中起作用