【问题标题】:PyQt4: How to load compiled ui-files correctly?PyQt4:如何正确加载已编译的 ui 文件?
【发布时间】: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


    【解决方案1】:

    也许我有一个解决方案,但我不知道这是否是一个完美的 Pythonic。

    好吧,我们知道所有 python 项目都有一个 __ init __ 文件。我们需要它来初始化 Python 包,对吧?好吧,我想:为什么不使用这个文件?我做了什么?我在 __ init __ -file 中定义了一个函数,如下所示:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    from PyQt4.uic import loadUi
    from PyQt4.QtCore import Qt, QFile
    
    def ui_load_about(self):
        uiFile = QFile(":/ui_file/about.ui")
        uiFile.open(QFile.ReadOnly)
        self.ui_about = loadUi(uiFile)
        uiFile.close()
    
        return self.ui_about
    

    现在在我的“About_Window”类中,我这样做:

    #!/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
    
    import __init__ as ui_file
    
    class About_Window(QDialog):
        def __init__(self, parent):
    
            QDialog.__init__(self, parent)
    
            self.ui_about = ui_file.ui_load_about(self)
    

    您看到我将包文件 (__ init __-file) 导入为 ui_file,然后我调用函数并将函数的返回保存在变量 self.ui_about 中。

    在我的例子中,我从 MainWindow(QMainWindow) 打开 About_Window,它看起来像这样:

    def create_about_form(self):
        self.ui_about = About_Window(self)
    
        # Now when I try to show (show()-method) a window I get two windows
        # The reason is: I open and load the ui files from compiled
        # qt resorce file that was define in __init__-module. 
        # There is a function that opens the resource file, reads  
        # the ui file an closes and returns the ui file back
    
        # That's the reason why I have commented out this method
        #self.ui_about.show()
    

    你看我注释掉了 show() 方法。没有这种方法也可以。我只定义 About_Window() 类。好吧,我知道这可能不是最好的解决方案,但它确实有效。我可以一次又一次地打开窗口而无需回溯。

    如果您有更好的解决方案或想法,请告诉我 :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 2016-01-25
      相关资源
      最近更新 更多