2013-02-07 2 views
6

QT 디자이너를 사용하여 PyQT에서 많은 GUI 인터페이스를 만들었지 만 다른 인터페이스를 열려고하는데 현재 어떻게해야할지 모르겠다. Start.py는 GUI 인터페이스를 실행 파일입니다 Authentification_1Acceuil_start.py은, 지금은 Start.py에서 점심 Acceuil_start하려는 GUI 인터페이스 Acceuil_2.py을 실행 파일입니다 .py. 그것에 대해 알고 있습니까? 고맙습니다. 여기 내 코드입니다 :다른 파일에서 GUI 파일 열기 PyQT

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_()) 
, 당신은 그들이 다른 이름, 그리고 일반적인 하나가 귀하의 GUI 클래스의 이름을 지정해야

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

답변

4

첫째, 당신이 그들을 구별 할 수 있도록.

왜 그렇게해야합니까? 음, 간단히 말해서, 모든 클래스가 다른 유형의 대화를 나타내는 경우 다른 유형이므로 이름을 다르게 지정해야합니다. 일부 이름은/QMessageBox, AboutBox, AddUserDialog 등이 될 수 있습니다.

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() #??? 

부모의 문제에 관하여 : 위젯/윈도우 인 경우 부모가 될 만든 객체를 설정, 다른 위젯을 만드는 것은 항상 (떨어져 일부 단수의 경우에서) 좋은 생각, 그리고 왜 당신이 볼 수 this을 읽어야 그래서 :

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.

편집 - 최소 작업 샘플

간단한 예제를 만들었습니다. MainWindowChildWindow의 두 클래스가 있습니다. 모든 클래스는 별도의 QApplication 개체를 만들어 다른 클래스없이 작업 할 수 있습니다. 하지만 MainWindowChildWindow을 가져 오는 경우 5 초 안에 트리거되는 슬롯에 ChildWindow 슬롯이 생성됩니다.

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

감사합니다. Is 부모 클래스, 다른 클래스를 열어야하는 클래스? _Acceuil_start.py_에서 클래스 이름을 변경해야하는 이유는 무엇입니까? – Copernic

+0

@Mehdi 답변을 업데이트하겠습니다! 이게 지금까지 작동합니까? –

+0

아니, 그것은 내게이 오류가 발생합니다 : _NameError : 전역 이름 'Acceuil'정의되지 않았습니다 :'self.Acceuil = Acceuil (자체)' – Copernic

0

Start.py에서 다른 대화 상자를 참조하려면, 당신은이 경우에 모듈 이름하여 접두사해야한다 Acceuil_start. 따라서 각 모듈에 중복 된 함수 이름이 있으면 정상입니다.그래서, 당신은 것입니다 :

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

을하지만, 당신이이 같은 프로세스에서 실행 두 app 객체를 가질 수 없습니다 명심하십시오. Acceuil_start.py를 주 윈도우가 아닌 대화 상자처럼 구성하려고 할 것입니다. 두 개의 별개 메인 윈도우 인 경우 Acceuil_start.py를 매개 변수로 사용하여 다른 Python 인터프리터를 호출하는 것이 더 쉽습니다.

+0

고마워요. 그것은 나를 준다'AttributeError : 'Acceuil'객체에는 'exec_'' 속성이 없다. – Copernic

관련 문제