問題描述
我想添加啟動窗口,當我單擊按鈕時,它將打開另一個窗口并關閉當前窗口.對于每個窗口,它都有從 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 文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!