PyQt:如何从 Qt Designer 加载多个 .ui 文件

时间:2023-03-12
本文介绍了PyQt:如何从 Qt Designer 加载多个 .ui 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想添加启动窗口,当我单击按钮时,它将打开另一个窗口并关闭当前窗口.对于每个窗口,它都有从 Qt Designer 以 .ui 形式创建的独立 UI.

I want to add startup window that when I click button, it will open another window and close current window. For each window, it has seperated UI which created from Qt Designer in .ui form.

我通过 uic.loadUiType() 加载两个 .ui 文件.第一个窗口(第一个 UI)通常可以显示其 UI,但是当我单击按钮转到另一个窗口时,另一个 UI(第二个 UI)不起作用.它喜欢打开空白窗口.

I load both .ui file via uic.loadUiType(). The first window(first UI) can normally show its UI but when I click button to go to another window, another UI (second UI) doesn't work. It likes open blank window.

另一个问题是,如果我加载第一个 UI,然后更改为第二个 UI(删除该类并更改为另一个类,同时删除 uic.loadUiType()),第二个 UI 仍然不起作用(显示空白窗口)

Another problem is if I load first UI and then change to second UI (delete that Class and change to another Class, also delete uic.loadUiType()), the second UI still doesn't work (show blank window)

请帮助...我在创建此问题之前进行了研究,但找不到答案.

Please help... I research before create this question but can't find the answer.

这是我的代码.我该如何解决?

Here's my code. How can I fix it?

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon
from PyQt5 import uic

#load both ui file
uifile_1 = 'UI/openPage.ui'
form_1, base_1 = uic.loadUiType(uifile_1)

uifile_2 = 'UI/mainPage.ui'
form_2, base_2 = uic.loadUiType(uifile_2)

class Example(base_1, form_1):
    def __init__(self):
        super(base_1,self).__init__()
        self.setupUi(self)
        self.startButton.clicked.connect(self.change)

    def change(self):
        self.main = MainPage()
        self.main.show()

class MainPage(base_2, form_2):
    def __int__(self):
        super(base_2, self).__init__()
        self.setupUi(self)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

推荐答案

首先出现错误,必须将__int__改为__init__.要关闭窗口,请调用 close() 方法.

First you have an error, you must change __int__ to __init__. To close the window call the close() method.

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon
from PyQt5 import uic

#load both ui file
uifile_1 = 'UI/openPage.ui'
form_1, base_1 = uic.loadUiType(uifile_1)

uifile_2 = 'UI/mainPage.ui'
form_2, base_2 = uic.loadUiType(uifile_2)

class Example(base_1, form_1):
    def __init__(self):
        super(base_1,self).__init__()
        self.setupUi(self)
        self.startButton.clicked.connect(self.change)

    def change(self):
        self.main = MainPage()
        self.main.show()
        self.close()

class MainPage(base_2, form_2):
    def __init__(self):
        super(base_2, self).__init__()
        self.setupUi(self)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

这篇关于PyQt:如何从 Qt Designer 加载多个 .ui 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何使用pyqtgraph TimeAxisItem使X轴时间动态刷新 下一篇:如何在 python 和 qml 中自动插入/编辑 QAbstractListModel 更新?

相关文章