簡體   English   中英

從另一個文件PyQT打開一個GUI文件

[英]Open a GUI file from another file PyQT

我使用QT Designer在PyQT中創建了許多GUI界面,但現在我正在嘗試從另一個界面打開一個界面,我不知道該怎么做.. Start.py是運行GUI界面的文件Authentification_1Acceuil_start.py是運行GUI界面Acceuil_2.py的文件,現在我想從Start.pyAcceuil_start.py午餐。 你對此有什么想法嗎? 謝謝。 這是我的代碼:

Start.py:

import sys
from PyQt4 import QtCore, QtGui
from Authentification_1 import Ui_Fenetre_auth
from Acceuil_2 import Ui_MainWindow #???  Acceuil_2.py is the file which I want to open

class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Fenetre_auth()
        self.ui.setupUi(self)

    def authentifier(val): #Slot method
        self.Acceuil = Acceuil() #???
        self.Acceuil.show() #???


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())

Acceuil_start.py

import sys
from PyQt4 import QtCore, QtGui
from Authentification_1 import Ui_Fenetre_auth
from Acceuil_2 import Ui_MainWindow

class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())

首先,您應該為GUI類命名,以便它們具有不同的名稱,而不是通用名稱,因此您可以區分它們。

為什么你需要這樣做? 嗯 - 簡單地說,因為它是有意義的 - 如果每個類代表不同類型的對話,那么它是不同的類型 - 它應該以不同的名稱命名。 一些名稱是/可能是: QMessageBoxAboutBoxAddUserDialog等。

在Acceuil_start.py中(您也應該在其他模塊中重命名類)。

import sys
from PyQt4 import QtCore, QtGui
from Authentification_1 import Ui_Fenetre_auth
from Acceuil_2 import Ui_MainWindow

class Acceuil(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = Acceuil()
    myapp.show()
    sys.exit(app.exec_())

在父類中,當你想要創建窗口時,你就近了(但它應該在任何情況下都有效):

def authentifier(val): #Slot method
    self.Acceuil = Acceuil(self) # You should always pass the parent to the child control
    self.Acceuil.show() #???

關於父問題:如果您的窗口小部件/窗口正在創建另一個窗口小部件,將創建者對象設置為父窗口總是一個好主意(除了一些單個案例),您應該閱讀此內容以了解為什么會這樣:

QObjects在對象樹中組織自己。 當您使用另一個對象作為父對象創建QObject時,它將添加到父對象的children()列表中,並在父對象時刪除。 事實證明,這種方法很好地滿足了GUI對象的需求。 例如,QShortcut(鍵盤快捷鍵)是相關窗口的子項,因此當用戶關閉該窗口時,也會刪除shorcut。

編輯 - 最小工作樣本

為了看看我想告訴你什么,我已經建立了一個簡單的例子。 你有兩個類 - MainWindowChildWindow 通過創建單獨的QApplication對象,每個類可以在沒有其他類的情況下工作 但是,如果你在MainWindow導入ChildWindow ,你將在連接到singleshot計時器的插槽中創建ChildWindow ,它將在5秒內觸發。

MainWindow.py:

import sys
from PyQt4 import QtCore, QtGui
from ChildWindow import ChildWindow

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        QtCore.QTimer.singleShot(5000, self.showChildWindow)


    def showChildWindow(self):
        self.child_win = ChildWindow(self)
        self.child_win.show()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MainWindow()
    myapp.show()
    sys.exit(app.exec_())

ChildWindow.py:

import sys
from PyQt4 import QtCore, QtGui

class ChildWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setWindowTitle("Child Window!")


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = ChildWindow()
    myapp.show()
    sys.exit(app.exec_())

要從Start.py引用其他對話框,必須在模塊名稱前加上前綴,在本例中為模塊名稱,即Acceuil_start。 因此,如果每個模塊中存在重復的函數名稱,則可以。 所以,你會有:

def authentifier(val): #Slot method
    dlg = Acceuil_start.StartQT4()
    dlg.exec_()

但是,如果您希望這些操作從同一進程運行,請記住您不能擁有兩個app對象。 您可能希望將Acceuil_start.py構建為對話框,而不是主窗口。 如果它們是兩個不同的主窗口,您可能會發現使用Acceuil_start.py作為參數調用另一個Python解釋器更容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM