PyQt4:如何正确加载已编译的 ui 文件?
PyQt4: How to load compiled ui-files correctly?
我创建了一个包含我所有 ui 文件的 Qt 资源文件,我在 python 文件中使用 pyrcc4 命令行编译了它,然后我加载了 ui 文件使用 loadUi。举个例子:
#!/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-window 一次时,一切正常。关闭 window 后,我尝试再次打开相同的 GUI-window,我得到了很长的回溯。
Traceback (most recent call last): File "D:\Dan\Python\xarphus\xarphus\frm_mdi.py", line 359, in
create_update_form self.update_form = Update_Window(self) File
"D:\Dan\Python\xarphus\xarphus\frm_update.py", line 135, in init
self.ui_update = loadUi(UI_PATH, self) File
"C:\Python27\lib\site-packages\PyQt4\uic__init__.py", line 238, in
loadUi return DynamicUILoader(package).loadUi(uifile, baseinstance,
resource_suffix) File
"C:\Python27\lib\site-packages\PyQt4\uic\Loader\loader.py", line 71,
in loadUi return self.parse(filename, resource_suffix, basedir) File
"C:\Python27\lib\site-packages\PyQt4\uic\uiparser.py", line 984, in
parse document = parse(filename) File
"C:\Python27\lib\xml\etree\ElementTree.py", line 1182, in parse
tree.parse(source, parser) File
"C:\Python27\lib\xml\etree\ElementTree.py", line 657, in parse
self._root = parser.close() File
"C:\Python27\lib\xml\etree\ElementTree.py", line 1654, in close
self._raiseerror(v) File "C:\Python27\lib\xml\etree\ElementTree.py",
line 1506, in _raiseerror raise err xml.etree.ElementTree.ParseError:
no element found: line 1, column 0
大家可以帮帮我吗?
也许我有解决方案,但我不知道那是否完美 pythonic。
好吧,我们知道所有 python 项目都有一个 __ init __-file。我们需要它来初始化 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"-class 中,我这样做:
#!/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 然后我调用函数并保存 return 变量 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()-class。好吧,我知道这可能不是最好的解决方案,但它确实有效。我可以一次又一次地打开 window 而无需回溯。
如果您有更好的解决方案或想法,请告诉我:-)
我创建了一个包含我所有 ui 文件的 Qt 资源文件,我在 python 文件中使用 pyrcc4 命令行编译了它,然后我加载了 ui 文件使用 loadUi。举个例子:
#!/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-window 一次时,一切正常。关闭 window 后,我尝试再次打开相同的 GUI-window,我得到了很长的回溯。
Traceback (most recent call last): File "D:\Dan\Python\xarphus\xarphus\frm_mdi.py", line 359, in create_update_form self.update_form = Update_Window(self) File "D:\Dan\Python\xarphus\xarphus\frm_update.py", line 135, in init self.ui_update = loadUi(UI_PATH, self) File "C:\Python27\lib\site-packages\PyQt4\uic__init__.py", line 238, in loadUi return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix) File "C:\Python27\lib\site-packages\PyQt4\uic\Loader\loader.py", line 71, in loadUi return self.parse(filename, resource_suffix, basedir) File "C:\Python27\lib\site-packages\PyQt4\uic\uiparser.py", line 984, in parse document = parse(filename) File "C:\Python27\lib\xml\etree\ElementTree.py", line 1182, in parse tree.parse(source, parser) File "C:\Python27\lib\xml\etree\ElementTree.py", line 657, in parse self._root = parser.close() File "C:\Python27\lib\xml\etree\ElementTree.py", line 1654, in close self._raiseerror(v) File "C:\Python27\lib\xml\etree\ElementTree.py", line 1506, in _raiseerror raise err xml.etree.ElementTree.ParseError: no element found: line 1, column 0
大家可以帮帮我吗?
也许我有解决方案,但我不知道那是否完美 pythonic。
好吧,我们知道所有 python 项目都有一个 __ init __-file。我们需要它来初始化 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"-class 中,我这样做:
#!/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 然后我调用函数并保存 return 变量 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()-class。好吧,我知道这可能不是最好的解决方案,但它确实有效。我可以一次又一次地打开 window 而无需回溯。
如果您有更好的解决方案或想法,请告诉我:-)