Open image file from another PyQT file

I have created many GUI interfaces in PyQT using QT Designer, but now I am trying to open the interface from another and I do not know how to do this. Start.py is the file that runs the Authentification_1 GUI interface and Acceuil_start.py is the file that runs the Acceuil_2 GUI interface. py , now I want from Start.py to dinner Acceuil_start.py . Do you have any idea about this? Thanks. Here is 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_()) 
+6
source share
2 answers

First, you must name your GUI classes so that they have a different name, not a common one, so that you can distinguish them.

Why do you need this? Well, simply because it makes sense - if each class represents a different type of dialogue, then it is a different type - and it should be called differently. Some names are / could be: QMessageBox , AboutBox , AddUserDialog , etc.

In Acceuil_start.py (you must also rename the class to another module).

 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 a window, you are close (but it should work anyway):

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

About the parent problem: if your widget / window creates another widget, setting the creator object as a parent is always a good idea (except in individual cases), and you should read this to understand why this is:

QObjects are organized in object trees. When you create a QObject with another object as a parent, it is added to the children () parent list and deleted when the parent object. It turns out that this approach is very well suited for the needs of GUI objects. For example, QShortcut (keyboard shortcut) is a child of the corresponding window, so when the user closes this window, shorcut is also deleted.

Edit - minimum working example

To find out what I'm trying to tell you, I built a simple example. You have two classes - MainWindow and ChildWindow . Each class can work without another class, creating separate QApplication objects. But if you import ChildWindow into MainWindow , you will create ChildWindow in the slot connected to the timer timer, which will start 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_()) 
+4
source

To link to another dialog box from Start.py, you must prefix it with the module name, which in this case is Acceuil_start. Thus, it is normal if there are duplicate function names in each module. So you will have:

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

However, if you want them to start from the same process, keep in mind that you cannot have two app objects. You will probably want to structure Acceuil_start.py to act as a dialog, rather than the main window. If these are two different main windows, it might be easier for you to simply call another Python interpreter with the Acceuil_start.py parameter as the parameter.

0
source

All Articles