通过类加载Pyqt5 UI

发布于 2025-02-09 17:32:54 字数 1934 浏览 4 评论 0原文

我目前正在尝试使用PYQT5更新我的Pyside代码。而且我有一个称为“ loader.py”的类,该类曾经从“ pyside.qtuitools”中使用“ quiloader”,但据我所知,在pyqt5中,此模块已通过“ uic”更改。

因此,这里的问题是我更改了从“ uic”中导入的“ quiloader”,但我总是会收到此错误:

ui_loader.py.py”,第4行,IN< module> class> class Uiloader(uic):typeerror:typeError:typeErlor:module:module( )最多有2个参数(3给定)

pyside中的原始代码

这是我得到代码对于我的Pyside App

pyqt5

ui_loader.py.py < /em>

from PyQt5 import uic
from PyQt5.QtCore import QMetaObject

class UiLoader(uic):
    def __init__(self, base_instance):
        uic.__init__(self, base_instance)
        self.base_instance = base_instance

    def createWidget(self, class_name, parent=None, name=''):
        if parent is None and self.base_instance:
            return self.base_instance
        else:
            # create a new widget for child widgets
            widget = uic.createWidget(self, class_name, parent, name)
            if self.base_instance:
                setattr(self.base_instance, name, widget)
            return widget

def load_ui(ui_file, base_instance=None):
    loader = UiLoader(base_instance)
    widget = loader.load(ui_file)
    QMetaObject.connectSlotsByName(widget)
    return widget

main.py

from PyQt5.QtWidgets import QMainWindow, QApplication
from ui_loader import load_ui
import sys

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        load_ui('my_interface.ui', self)

def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()

我也尝试使用班级的Sbordethod并重构所有代码,但没有用。

I'm currently trying to update my PySide code using PyQt5. And I have a class called "loader.py" that used to use "QUiLoader" from "PySide.QtUiTools", but as far as I know in PyQt5 this module has been changed by "uic".

So the problem here is that I changed my "QUiLoader" import from "uic" but I always get this error:

ui_loader.py", line 4, in <module> class UiLoader(uic): TypeError: module() takes at most 2 arguments (3 given)

Original Code in Pyside

Here is where I got the code for my PySide app

Code in PyQt5

ui_loader.py

from PyQt5 import uic
from PyQt5.QtCore import QMetaObject

class UiLoader(uic):
    def __init__(self, base_instance):
        uic.__init__(self, base_instance)
        self.base_instance = base_instance

    def createWidget(self, class_name, parent=None, name=''):
        if parent is None and self.base_instance:
            return self.base_instance
        else:
            # create a new widget for child widgets
            widget = uic.createWidget(self, class_name, parent, name)
            if self.base_instance:
                setattr(self.base_instance, name, widget)
            return widget

def load_ui(ui_file, base_instance=None):
    loader = UiLoader(base_instance)
    widget = loader.load(ui_file)
    QMetaObject.connectSlotsByName(widget)
    return widget

main.py

from PyQt5.QtWidgets import QMainWindow, QApplication
from ui_loader import load_ui
import sys

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        load_ui('my_interface.ui', self)

def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()

I have also tried to used submethod of the class and refactoring all the code but it was useless.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清欢 2025-02-16 17:32:54

您实际上可以使用UIC加载UI,然后将UI继承到类中,然后将UI继承为Self.SetUpui(),

ui_MainWindow, QtBaseClass = uic.loadUiType("main_window.ui")

class MainWindow(QMainWindow, ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)

因为MusicAmante建议将其像这样:

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("main_window.ui", self)

You can actually load the ui using uic and then just inherit that ui into a class then self.setupUI()

ui_MainWindow, QtBaseClass = uic.loadUiType("main_window.ui")

class MainWindow(QMainWindow, ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)

Another way as musicamante is suggesting would be like this:

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("main_window.ui", self)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文