简体   繁体   中英

Open a GUI file from another file PyQT

I've created many GUI interfaces in PyQT using QT Designer, but now I'm trying to open an interface from another one, and I don't know how to do it.. Start.py is the file which run the GUI Interface Authentification_1 and Acceuil_start.py is the file which run the GUI interface Acceuil_2.py , now I want from Start.py to lunch Acceuil_start.py . Do you have any idea about that ? Thank you. Here's my code :

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_())

First, you should to name your GUI classes so they have a different name, and not the generic one, so you could distinct them.

Why you would need to do that? Well - simply, because it makes sense - if every class is representing different type of dialog, so it is the different type - and it should be named differently. Some of the names are/may be: QMessageBox , AboutBox , AddUserDialog , and so on.

In Acceuil_start.py (you should rename class in other module, too).

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_())

in the parent class, when you want to create the window, you are close (but it should work in any case):

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

About parent issue: If your widget/window is creating another widget, setting creator object to be parent is always a good idea (apart from some singular cases), and you should read this to see why is that so:

QObjects organize themselves in object trees. When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is. It turns out that this approach fits the needs of GUI objects very well. For example, a QShortcut (keyboard shortcut) is a child of the relevant window, so when the user closes that window, the shorcut is deleted too.

Edit - Minimal Working Sample

To see what I am trying to tell you, I've built a simple example. You have two classes - MainWindow and ChildWindow . Every class can work without other class by creating separate QApplication objects. But, if you import ChildWindow in MainWindow , you will create ChildWindow in slot connected to singleshot timer which will trigger in 5 seconds.

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_())

To reference the other dialog from Start.py, you must prefix it by the module name, which in this case is Acceuil_start. As such, it is OK if there are duplicated function names in each module. So, you would have:

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

However, if you want these to run from the same process, keep in mind that you can't have two app objects. You would probably want to structure Acceuil_start.py to act like a dialog, rather than a main window. If these are two distinct main windows, you might find it easier to just invoke another Python interpreter with the Acceuil_start.py as a parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM