2017-01-13 2 views
0

나는 python project.and가 있습니다. pyqt4에 새로 왔지만 문제가 발생하여 해결 방법을 찾을 수 없습니다. 내 프로젝트에 main.py라는 메인 파일이 있고 그 내용은 다음과 같습니다.파일을 실행 한 후 python pyq4 widget exit

import os 
import sys 

from PyQt4 import QtGui, uic 
import setup, modules 


class MyWindow(QtGui.QMainWindow): 
    def __init__(self): 
     QtGui.QWidget.__init__(self) 
     super(MyWindow, self).__init__() 
     file_path = os.path.abspath("form_ui/main_window.ui") 
     uic.loadUi(file_path, self) 
     self.show() 
     self.statusBar().showMessage('Developer contact: [email protected]')    #message on status bar of main window 
     self.actionExit.triggered.connect(self.close)            #action to close main windows 
     self.actionSetup.triggered.connect(setup.setup_gui.setupgui)        #action to run setup_guy.py 
     self.actionUser.triggered.connect(modules.user_form.user_set)        #action to run user_form.py 
     self.actionClient_check.triggered.connect(modules.client_read.client_read_op)    #action to rin client_read.py 
     self.actionClient_add.triggered.connect(modules.client_make.client_make_op)    #action to run client_make.py 
     self.actionEquipment_add.triggered.connect(modules.add_equipment.add_equipment_op)   #action to run add_equipment.py 
     self.actionOrders.triggered.connect(modules.comanda.comanda_def)       #action to run comanda.py 
     self.actionServices.triggered.connect(modules.services.add_services_op)     #action to run services.py 
     self.actionList_data.triggered.connect(modules.list_data.list_data_op)      #action to run list_data.py 
     self.move(QtGui.QApplication.desktop().screen().rect().center()- self.rect().center())  #center main windows 

if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    window = MyWindow() 
    window.show() 
    sys.exit(app.exec_()) 

앱이 시작되고 GUI가 실행됩니다. 각 작업은 qwidget을 시작합니다. 이것은 하나는 QWidget 예입니다

import sys,os.path 
from PyQt4 import QtGui, uic 

class user_set(QtGui.QWidget): 
    def __init__(self): 
     QtGui.QWidget.__init__(self) 
     super(user_set, self).__init__() 
     file_path = os.path.abspath("form_ui/user_form.ui") 
     uic.loadUi(file_path, self) 
     self.show() 
     self.setFixedSize(self.size()) 
     self.move(QtGui.QApplication.desktop().screen().rect().center() - self.rect().center()) 
     sys.exit(app) 

if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    window = user_set() 
    window.show() 
    sys.exit(app.exec_()) 

메인 윈도우는 즉시 종료 위젯 (위젯하지 메인 윈도우)를 실행하려고하면. 나는 sys.exit(app) 라인을 삽입했고 위젯은 ok를 실행하지만 pycharm에서는 sys.exit(app) NameError: global name 'app' is not defined이 발생합니다. 위젯 파일 만 실행하려고하면 윈도우가 나타나서 instantlly를 종료하고 sys.exit(app) 줄을 주석 처리해야합니다.

누군가 내가 잘못한 이상이 있습니까? 고마워요

EDIT1 ------------------------------------------- ------------------------

그의 내 주요 파일입니다

:

import os 
import sys 

from PyQt4 import QtGui, uic 
import setup, modules 


class MyWindow(QtGui.QMainWindow): 
    def __init__(self): 
     QtGui.QWidget.__init__(self) 
     super(MyWindow, self).__init__() 
     file_path = os.path.abspath("form_ui/main_window.ui") 
     uic.loadUi(file_path, self) 
     self.show() 
     self.statusBar().showMessage('Developer contact: [email protected]')    #message on status bar of main window 
     self.actionExit.triggered.connect(self.close)            #action to close main windows 
     self.actionSetup.triggered.connect(self.run_dialog_setup)        #action to run setup_guy.py 
     self.actionUser.triggered.connect(self.run_dialog_user_set)        #action to run user_form.py 
     self.actionClient_check.triggered.connect(self.run_dialog_client_read)    #action to rin client_read.py 
     self.actionClient_add.triggered.connect(self.run_dialog_client_make)    #action to run client_make.py 
     self.actionEquipment_add.triggered.connect(self.run_dialog_add_equipment)   #action to run add_equipment.py 
     self.actionOrders.triggered.connect(self.run_dialog_comanda)       #action to run comanda.py 
     self.actionServices.triggered.connect(self.run_dialog_add_services)     #action to run services.py 
     self.actionList_data.triggered.connect(self.run_dialog_list_data)      #action to run list_data.py 
     self.move(QtGui.QApplication.desktop().screen().rect().center()- self.rect().center())  #center main windows 


    def run_dialog_user_set(self): 
     dialog = modules.user_form.user_set() 
     dialog.show() 
     dialog.accept() 
     sys.exit(dialog.exec_()) 
if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    # app.setQuitOnLastWindowClosed(False) 
    # QtGui.QApplication.setQuitOnLastWindowClosed(False) 
    window = MyWindow() 
    window.show() 
    sys.exit(app.exec_()) 

내 두 번째 파일이 있습니다

import sys,os.path 
from PyQt4 import QtGui, uic 

class user_set(QtGui.QDialog): 
    def __init__(self): 
     QtGui.QDialog.__init__(self) 
     file_path = os.path.abspath("form_ui/user_form.ui") 
     uic.loadUi(file_path, self) 
     self.setFixedSize(self.size()) 
     self.move(QtGui.QApplication.desktop().screen().rect().center() - self.rect().center()) 

# if __name__ == '__main__': 
#  app = QtGui.QApplication(sys.argv) 
#  window = user_set() 
#  window.show() 
#  sys.exit(app.exec_()) 

나는 PYPL 솔루션을 시도했으며 앱 오류가 해결되었습니다. 나는 그의 마지막 해결책을 시도했지만 아무 결과도, 애플 리케이션 stil 폐쇄입니다. 나는 내가 의미하는 것을보기 위해 youtube video에 링크를 설정할 것이다.

추 신 : 나는 그것을 이해하지 못했기 때문에 그것을 테스트 할 수 없었습니다. 나는 Pyhton을 처음 접했고 영어 원어민이 아니 었습니다. 미안 해요.

유튜브 링크 : https://youtu.be/9F2-5NVaqvQ

+0

위젯 안에'sys.exit (app) '이 있으므로 즉시 종료해야합니다. 그러나이 코드를 가져 오면'if __name__ == '__main __':'을 실행하지 않고'app'을 생성하지 않고'sys.exit (app)'를 작성하면 오류가 발생합니다. 어떤 부분이 실행되는지 보려면'print()'를 코드에 넣으십시오. BTW : 위젯은 메인 파일에 생성 된'app'에 접근 할 수 없습니다. – furas

+0

BTW : 클래스에'CamelCase' 이름을 사용하세요 -'UserSet' -'trigger.connect()'에서 클래스 이름을 사용하는 것처럼 보이지만 함수/메소드 만 기대할 수 있습니다. – furas

+0

귀하의 의견을 조사하고 있습니다. 테스트 해보십시오. 나는 대답으로 돌아올거야. –

답변

0

당신은 두 클래스에 연속으로 두 번 슈퍼 클래스의 초기화가! QtGui.QDialog

import sys,os.path 
from PyQt4 import QtGui, uic 

class user_set(QtGui.QDialog): 
    def __init__(self): 
     QtGui.QWidget.__init__(self) init super [1] 
     super(user_set, self).__init__() init super [2] -> GET RID OF THIS !! 
     file_path = os.path.abspath("form_ui/user_form.ui") 
     uic.loadUi(file_path, self) 
     self.setFixedSize(self.size()) 
     self.move(QtGui.QApplication.desktop().screen().rect().center() - self.rect().center()) 

변경 user_set 슈퍼 당신은 당신의 클래스 __init__ 기능에 .show()를 호출하지 않아야합니다! 대화 상자를 만들고 위젯을 호출하는 또 다른 메소드를 생성하십시오.

class MyWindow(QtGui.QMainWindow): 
    def __init__(self): 
     QtGui.QWidget.__init__(self) 
     file_path = os.path.abspath("form_ui/main_window.ui") 
     uic.loadUi(file_path, self) 
     self.statusBar().showMessage('Developer contact: [email protected]') 
     self.actionExit.triggered.connect(self.close) 
     self.actionSetup.triggered.connect(setup.setup_gui.setupgui) 
     self.actionUser.triggered.connect(self.run_dialog_user_set) 
     self.actionClient_check.triggered.connect(modules.client_read.client_read_op) 
     self.actionClient_add.triggered.connect(modules.client_make.client_make_op) 
     self.actionEquipment_add.triggered.connect(modules.add_equipment.add_equipment_op) 
     self.actionOrders.triggered.connect(modules.comanda.comanda_def) 
     self.actionServices.triggered.connect(modules.services.add_services_op) 
     self.actionList_data.triggered.connect(modules.list_data.list_data_op) 
     self.move(QtGui.QApplication.desktop().screen().rect().center()- self.rect().center()) 

    def run_dialog_user_set(self): 
     dialog = modules.user_form.user_set() 
     dialog.show() 
+0

수정했지만 창은 즉시 열리고 닫힙니다. 나는 user_set 클래스를 Qdialog로 바꾼다. user_set 클래스에서 sys.exit (app) 행의 주석을 제거하면 창이 열리지 만 콘솔의 오류 메시지가 나타납니다. 이것은 오류입니다 : NameError : 글로벌 이름 'app'이 정의되지 않았습니다. 어떤 생각입니까? –

+0

문제가 다소 해결되었지만 솔루션을 조금 조정했습니다. 이것은 내가 사용하는 것입니다 : 'def run_dialog_user_set (self) : dialog = modules.user_form.user_set() dialog.show() sys.exit (dialog.exec _())'.'user_set'이 열리고 stai가 열렸지만 닫으면 주 창이 닫힙니다. –

+0

@BogdanRadu 그 이유는 qappliaction을 참조하려고하는 대화창에서'self.move()'를하고 있기 때문일 수 있습니다. 'run_dialog_user_set' 메쏘드에서'dialog.show()'를 호출하기 직전에 당신의 대화창을'.move'합니다 :'dialog.move ()' – PYPL