【发布时间】:2015-09-26 12:07:24
【问题描述】:
我创建了一个 Qt 资源文件,其中包含我的所有 ui 文件,我在 python 文件中使用 pyrcc4 命令行编译了它,然后我使用 loadUi 加载了 ui 文件。举个例子:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os
import sys
from PyQt4.QtCore import Qt, QFile
from PyQt4.uic import loadUi
from PyQt4.QtGui import QDialog
from xarphus.gui import ui_rc
# I import the compiled qt resource file named ui_rc
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
#UI_PATH = os.path.join(BASE_PATH, 'gui', 'create_user.ui')
UI_PATH = QFile(":/ui_file/create_user.ui")
# I want to load those compiled ui files,
# so I just create QFile.
class CreateUser_Window(QDialog):
def __init__(self, parent):
QDialog.__init__(self, parent)
# I open the created QFile
UI_PATH.open(QFile.ReadOnly)
# I read the QFile and load the ui file
self.ui_create_user = loadUi(UI_PATH, self)
# After then I close it
UI_PATH.close()
它工作正常,但我有一个问题。当我打开 GUI 窗口一次时,一切正常。关闭窗口后,我尝试再次打开相同的 GUI 窗口,我得到了很长的回溯。
回溯(最近一次调用最后):文件“D:\Dan\Python\xarphus\xarphus\frm_mdi.py”,第 359 行,在 create_update_form self.update_form = Update_Window(self) 文件 "D:\Dan\Python\xarphus\xarphus\frm_update.py",第 135 行,在 init 中 self.ui_update = loadUi(UI_PATH, self) 文件 “C:\Python27\lib\site-packages\PyQt4\uic__init__.py”,第 238 行,在 loadUi return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix) 文件 “C:\Python27\lib\site-packages\PyQt4\uic\Loader\loader.py”,第 71 行, 在 loadUi 返回 self.parse(filename, resource_suffix, basedir) 文件 “C:\Python27\lib\site-packages\PyQt4\uic\uiparser.py”,第 984 行,在 解析文档 = 解析(文件名)文件 “C:\Python27\lib\xml\etree\ElementTree.py”,第 1182 行,解析中 tree.parse(source, parser) 文件 “C:\Python27\lib\xml\etree\ElementTree.py”,第 657 行,解析中 self._root = parser.close() 文件 “C:\Python27\lib\xml\etree\ElementTree.py”,第 1654 行,最后 self._raiseerror(v) 文件 "C:\Python27\lib\xml\etree\ElementTree.py", 第 1506 行,在 _raiseerror 中引发错误 xml.etree.ElementTree.ParseError: 未找到元素:第 1 行,第 0 列
大家可以帮帮我吗?
【问题讨论】:
标签: qt python-2.7 pyqt4 loadui