2016-06-23 2 views
1

여기 처음 질문을하는 경우 실수가 있으면 용서해주십시오.QFileDialog는 전화 할 때 창을 닫습니다.

내 문제는 내 프로그램을 실행하면 잘 작동합니다. 그러나 검색을 클릭하자마자 프로그램이 닫힙니다.

나는 실행하는 기능을 호출하는 self.pushButton_5.cicked.connect(run)를 사용

openfile = QtGui.QFileDialog.getOpenFileName(self) # Filename line f = open(openfile, 'r') # New line data = f.read()

을이 내 프로그램이 종료되지 것입니다. 이걸 print('Hello')으로 바꾸면 제대로 작동합니다.

Qt Creator를 사용하여 GUI를 만들고 Pycharm을 사용하여 수정합니다. Python 3.5를 사용하여 Windows 7에서 PyQt5를 실행 중입니다. 나는이 (4-5 시간 정상)에 아주 새롭다 그러나 나는 1 년 내내 학교에있는 python를 사용하고있다 (아직도 아주 기본적인 그러나 나는 괜찮 이해있다). 이것은 연말 말의 상당 부분을 차지하는 수업 프로젝트의 경우에 해당됩니다.

내 전체 코드는 다음과 같습니다 : 당신은 비 멤버 함수에서 키워드 self을 사용하는

# -*- coding: utf-8 -*- 

# Form implementation generated from reading ui file 'input.ui' 
# 
# Created by: PyQt5 UI code generator 5.6 
# 
# WARNING! All changes made in this file will be lost! 

from PyQt5 import QtCore, QtGui, QtWidgets 

class Ui_MainWindow(object): 
    def setupUi(self, MainWindow): 
     MainWindow.setObjectName("MainWindow") 
     MainWindow.resize(764, 500) 
     MainWindow.setMinimumSize(QtCore.QSize(764, 353)) 
     MainWindow.setMaximumSize(QtCore.QSize(764, 500)) 
     self.centralWidget = QtWidgets.QWidget(MainWindow) 
     self.centralWidget.setObjectName("centralWidget") 
     self.verticalLayout = QtWidgets.QVBoxLayout(self.centralWidget) 
     self.verticalLayout.setContentsMargins(11, 11, 11, 11) 
     self.verticalLayout.setSpacing(6) 
     self.verticalLayout.setObjectName("verticalLayout") 
     self.pushButton_5 = QtWidgets.QPushButton(self.centralWidget) 
     self.pushButton_5.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor)) 
     self.pushButton_5.setObjectName("pushButton_5") 
     self.verticalLayout.addWidget(self.pushButton_5) 
     self.verticalLayout_4 = QtWidgets.QVBoxLayout() 
     self.verticalLayout_4.setContentsMargins(11, 11, 11, 11) 
     self.verticalLayout_4.setSpacing(6) 
     self.verticalLayout_4.setObjectName("verticalLayout_4") 
     self.label = QtWidgets.QLabel(self.centralWidget) 
     self.label.setFrameShape(QtWidgets.QFrame.NoFrame) 
     self.label.setFrameShadow(QtWidgets.QFrame.Plain) 
     self.label.setText("") 
     self.label.setPixmap(QtGui.QPixmap("../../Pictures/vlcsnap-2016-06-05-12h01m15s119.png")) 
     self.label.setScaledContents(True) 
     self.label.setObjectName("label") 
     self.verticalLayout_4.addWidget(self.label) 
     self.verticalLayout.addLayout(self.verticalLayout_4) 
     self.horizontalSlider = QtWidgets.QSlider(self.centralWidget) 
     self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal) 
     self.horizontalSlider.setObjectName("horizontalSlider") 
     self.verticalLayout.addWidget(self.horizontalSlider) 
     self.verticalLayout_2 = QtWidgets.QVBoxLayout() 
     self.verticalLayout_2.setContentsMargins(11, 11, 11, 11) 
     self.verticalLayout_2.setSpacing(6) 
     self.verticalLayout_2.setObjectName("verticalLayout_2") 
     self.horizontalLayout = QtWidgets.QHBoxLayout() 
     self.horizontalLayout.setContentsMargins(11, 11, 11, 11) 
     self.horizontalLayout.setSpacing(6) 
     self.horizontalLayout.setObjectName("horizontalLayout") 
     self.pushButton_3 = QtWidgets.QPushButton(self.centralWidget) 
     self.pushButton_3.setObjectName("pushButton_3") 
     self.horizontalLayout.addWidget(self.pushButton_3) 
     self.pushButton_2 = QtWidgets.QPushButton(self.centralWidget) 
     self.pushButton_2.setObjectName("pushButton_2") 
     self.horizontalLayout.addWidget(self.pushButton_2) 
     self.pushButton = QtWidgets.QPushButton(self.centralWidget) 
     self.pushButton.setObjectName("pushButton") 
     self.horizontalLayout.addWidget(self.pushButton) 
     self.horizontalLayout_3 = QtWidgets.QHBoxLayout() 
     self.horizontalLayout_3.setContentsMargins(11, 11, 11, 11) 
     self.horizontalLayout_3.setSpacing(6) 
     self.horizontalLayout_3.setObjectName("horizontalLayout_3") 
     self.dial = QtWidgets.QDial(self.centralWidget) 
     self.dial.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor)) 
     self.dial.setMouseTracking(True) 
     self.dial.setObjectName("dial") 
     self.horizontalLayout_3.addWidget(self.dial) 
     self.pushButton_4 = QtWidgets.QPushButton(self.centralWidget) 
     self.pushButton_4.setObjectName("pushButton_4") 
     self.horizontalLayout_3.addWidget(self.pushButton_4) 
     self.horizontalLayout.addLayout(self.horizontalLayout_3) 
     self.verticalLayout_2.addLayout(self.horizontalLayout) 
     self.verticalLayout.addLayout(self.verticalLayout_2) 
     MainWindow.setCentralWidget(self.centralWidget) 
     self.menuBar = QtWidgets.QMenuBar(MainWindow) 
     self.menuBar.setGeometry(QtCore.QRect(0, 0, 764, 21)) 
     self.menuBar.setObjectName("menuBar") 
     self.menuKMS = QtWidgets.QMenu(self.menuBar) 
     self.menuKMS.setObjectName("menuKMS") 
     self.menuScan_System = QtWidgets.QMenu(self.menuKMS) 
     self.menuScan_System.setObjectName("menuScan_System") 
     self.menuSort = QtWidgets.QMenu(self.menuKMS) 
     self.menuSort.setObjectName("menuSort") 
     self.menuSmall_Big = QtWidgets.QMenu(self.menuSort) 
     self.menuSmall_Big.setObjectName("menuSmall_Big") 
     self.menuBy_Resolution = QtWidgets.QMenu(self.menuSort) 
     self.menuBy_Resolution.setObjectName("menuBy_Resolution") 
     self.menuAlpha = QtWidgets.QMenu(self.menuSort) 
     self.menuAlpha.setObjectName("menuAlpha") 
     self.menuEdit = QtWidgets.QMenu(self.menuBar) 
     self.menuEdit.setObjectName("menuEdit") 
     self.menuFormats_2 = QtWidgets.QMenu(self.menuEdit) 
     self.menuFormats_2.setObjectName("menuFormats_2") 
     self.menuVolume = QtWidgets.QMenu(self.menuEdit) 
     self.menuVolume.setObjectName("menuVolume") 
     self.menuFont = QtWidgets.QMenu(self.menuEdit) 
     self.menuFont.setObjectName("menuFont") 
     self.menuHelp = QtWidgets.QMenu(self.menuBar) 
     self.menuHelp.setObjectName("menuHelp") 
     MainWindow.setMenuBar(self.menuBar) 
     self.statusBar = QtWidgets.QStatusBar(MainWindow) 
     self.statusBar.setObjectName("statusBar") 
     MainWindow.setStatusBar(self.statusBar) 
     self.actionAdd_Formats = QtWidgets.QAction(MainWindow) 
     self.actionAdd_Formats.setObjectName("actionAdd_Formats") 
     self.actionRemove_Formats = QtWidgets.QAction(MainWindow) 
     self.actionRemove_Formats.setObjectName("actionRemove_Formats") 
     self.actionHow_To_Use_KMS = QtWidgets.QAction(MainWindow) 
     self.actionHow_To_Use_KMS.setObjectName("actionHow_To_Use_KMS") 
     self.actionAbout_KMS = QtWidgets.QAction(MainWindow) 
     self.actionAbout_KMS.setObjectName("actionAbout_KMS") 
     self.actionReport_a_Bug = QtWidgets.QAction(MainWindow) 
     self.actionReport_a_Bug.setObjectName("actionReport_a_Bug") 
     self.actionChange_File_Name = QtWidgets.QAction(MainWindow) 
     self.actionChange_File_Name.setObjectName("actionChange_File_Name") 
     self.actionMove_File = QtWidgets.QAction(MainWindow) 
     self.actionMove_File.setObjectName("actionMove_File") 
     self.actionDelete_File = QtWidgets.QAction(MainWindow) 
     self.actionDelete_File.setObjectName("actionDelete_File") 
     self.actionView = QtWidgets.QAction(MainWindow) 
     self.actionView.setObjectName("actionView") 
     self.actionFont_Size = QtWidgets.QAction(MainWindow) 
     self.actionFont_Size.setObjectName("actionFont_Size") 
     self.actionAdd_Format = QtWidgets.QAction(MainWindow) 
     self.actionAdd_Format.setObjectName("actionAdd_Format") 
     self.actionRemove_Format = QtWidgets.QAction(MainWindow) 
     self.actionRemove_Format.setObjectName("actionRemove_Format") 
     self.actionDefault_Volume_2 = QtWidgets.QAction(MainWindow) 
     self.actionDefault_Volume_2.setObjectName("actionDefault_Volume_2") 
     self.actionVolume_Controller = QtWidgets.QAction(MainWindow) 
     self.actionVolume_Controller.setObjectName("actionVolume_Controller") 
     self.actionDefault_Font = QtWidgets.QAction(MainWindow) 
     self.actionDefault_Font.setObjectName("actionDefault_Font") 
     self.actionFont_Size_2 = QtWidgets.QAction(MainWindow) 
     self.actionFont_Size_2.setObjectName("actionFont_Size_2") 
     self.actionSmall_Large = QtWidgets.QAction(MainWindow) 
     self.actionSmall_Large.setObjectName("actionSmall_Large") 
     self.actionLarge_Small_2 = QtWidgets.QAction(MainWindow) 
     self.actionLarge_Small_2.setObjectName("actionLarge_Small_2") 
     self.actionHigh_Low = QtWidgets.QAction(MainWindow) 
     self.actionHigh_Low.setObjectName("actionHigh_Low") 
     self.actionLow_High = QtWidgets.QAction(MainWindow) 
     self.actionLow_High.setObjectName("actionLow_High") 
     self.actionA_Z = QtWidgets.QAction(MainWindow) 
     self.actionA_Z.setObjectName("actionA_Z") 
     self.actionZ_A = QtWidgets.QAction(MainWindow) 
     self.actionZ_A.setObjectName("actionZ_A") 
     self.actionScan_Entire_System = QtWidgets.QAction(MainWindow) 
     self.actionScan_Entire_System.setObjectName("actionScan_Entire_System") 
     self.actionScan_Specific_Loaction = QtWidgets.QAction(MainWindow) 
     self.actionScan_Specific_Loaction.setObjectName("actionScan_Specific_Loaction") 
     self.actionExit = QtWidgets.QAction(MainWindow) 
     self.actionExit.setObjectName("actionExit") 
     self.actionPrint = QtWidgets.QAction(MainWindow) 
     self.actionPrint.setObjectName("actionPrint") 
     self.menuScan_System.addAction(self.actionScan_Entire_System) 
     self.menuScan_System.addAction(self.actionScan_Specific_Loaction) 
     self.menuSmall_Big.addAction(self.actionSmall_Large) 
     self.menuSmall_Big.addAction(self.actionLarge_Small_2) 
     self.menuBy_Resolution.addAction(self.actionHigh_Low) 
     self.menuBy_Resolution.addAction(self.actionLow_High) 
     self.menuAlpha.addAction(self.actionA_Z) 
     self.menuAlpha.addAction(self.actionZ_A) 
     self.menuSort.addAction(self.menuSmall_Big.menuAction()) 
     self.menuSort.addAction(self.menuBy_Resolution.menuAction()) 
     self.menuSort.addAction(self.menuAlpha.menuAction()) 
     self.menuKMS.addAction(self.menuScan_System.menuAction()) 
     self.menuKMS.addAction(self.menuSort.menuAction()) 
     self.menuKMS.addSeparator() 
     self.menuKMS.addAction(self.actionPrint) 
     self.menuKMS.addSeparator() 
     self.menuKMS.addAction(self.actionExit) 
     self.menuFormats_2.addAction(self.actionAdd_Format) 
     self.menuFormats_2.addAction(self.actionRemove_Format) 
     self.menuVolume.addAction(self.actionDefault_Volume_2) 
     self.menuVolume.addAction(self.actionVolume_Controller) 
     self.menuFont.addAction(self.actionDefault_Font) 
     self.menuFont.addAction(self.actionFont_Size_2) 
     self.menuEdit.addAction(self.actionChange_File_Name) 
     self.menuEdit.addAction(self.actionMove_File) 
     self.menuEdit.addAction(self.actionDelete_File) 
     self.menuEdit.addSeparator() 
     self.menuEdit.addAction(self.menuFont.menuAction()) 
     self.menuEdit.addSeparator() 
     self.menuEdit.addAction(self.menuVolume.menuAction()) 
     self.menuEdit.addSeparator() 
     self.menuEdit.addAction(self.actionView) 
     self.menuEdit.addAction(self.menuFormats_2.menuAction()) 
     self.menuHelp.addAction(self.actionHow_To_Use_KMS) 
     self.menuHelp.addSeparator() 
     self.menuHelp.addAction(self.actionReport_a_Bug) 
     self.menuHelp.addSeparator() 
     self.menuHelp.addAction(self.actionAbout_KMS) 
     self.menuBar.addAction(self.menuKMS.menuAction()) 
     self.menuBar.addAction(self.menuEdit.menuAction()) 
     self.menuBar.addAction(self.menuHelp.menuAction()) 


     self.retranslateUi(MainWindow) 
     QtCore.QMetaObject.connectSlotsByName(MainWindow) 




    def retranslateUi(self, MainWindow): 
     _translate = QtCore.QCoreApplication.translate 
     MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) 
     self.pushButton_5.setText(_translate("MainWindow", "Search")) 
     self.pushButton_3.setText(_translate("MainWindow", "Fast-Forward")) 
     self.pushButton_2.setText(_translate("MainWindow", "Pause/Play")) 
     self.pushButton.setText(_translate("MainWindow", "Rewind")) 
     self.pushButton_4.setText(_translate("MainWindow", "Full Screen")) 
     self.menuKMS.setTitle(_translate("MainWindow", "File")) 
     self.menuScan_System.setTitle(_translate("MainWindow", "Scan System")) 
     self.menuSort.setTitle(_translate("MainWindow", "Sort")) 
     self.menuSmall_Big.setTitle(_translate("MainWindow", "By File Size")) 
     self.menuBy_Resolution.setTitle(_translate("MainWindow", "By Resolution")) 
     self.menuAlpha.setTitle(_translate("MainWindow", "Alphabetically")) 
     self.menuEdit.setTitle(_translate("MainWindow", "Edit")) 
     self.menuFormats_2.setTitle(_translate("MainWindow", "Formats")) 
     self.menuVolume.setTitle(_translate("MainWindow", "Volume")) 
     self.menuFont.setTitle(_translate("MainWindow", "Font")) 
     self.menuHelp.setTitle(_translate("MainWindow", "Help")) 
     self.actionAdd_Formats.setText(_translate("MainWindow", "Add Formats")) 
     self.actionRemove_Formats.setText(_translate("MainWindow", "Remove Formats")) 
     self.actionHow_To_Use_KMS.setText(_translate("MainWindow", "How To Use KMS")) 
     self.actionAbout_KMS.setText(_translate("MainWindow", "About KMS")) 
     self.actionReport_a_Bug.setText(_translate("MainWindow", "Report a Bug")) 
     self.actionChange_File_Name.setText(_translate("MainWindow", "Rename \"Movie\"")) 
     self.actionMove_File.setText(_translate("MainWindow", "Move \"Movie\"")) 
     self.actionDelete_File.setText(_translate("MainWindow", "Delete \"Movie\"")) 
     self.actionView.setText(_translate("MainWindow", "View")) 
     self.actionFont_Size.setText(_translate("MainWindow", "Font Size")) 
     self.actionAdd_Format.setText(_translate("MainWindow", "Add Format")) 
     self.actionRemove_Format.setText(_translate("MainWindow", "Remove Format")) 
     self.actionDefault_Volume_2.setText(_translate("MainWindow", "Default Volume")) 
     self.actionVolume_Controller.setText(_translate("MainWindow", "Volume Controller")) 
     self.actionDefault_Font.setText(_translate("MainWindow", "Change Font")) 
     self.actionFont_Size_2.setText(_translate("MainWindow", "Font Size")) 
     self.actionSmall_Large.setText(_translate("MainWindow", "Small > Large")) 
     self.actionLarge_Small_2.setText(_translate("MainWindow", "Large > Small")) 
     self.actionHigh_Low.setText(_translate("MainWindow", "High > Low")) 
     self.actionLow_High.setText(_translate("MainWindow", "Low > High")) 
     self.actionA_Z.setText(_translate("MainWindow", "A > Z")) 
     self.actionZ_A.setText(_translate("MainWindow", "Z > A")) 
     self.actionScan_Entire_System.setText(_translate("MainWindow", "Scan Entire System")) 
     self.actionScan_Specific_Loaction.setText(_translate("MainWindow", "Scan Specific Loaction")) 
     self.actionExit.setText(_translate("MainWindow", "Exit")) 
     self.actionPrint.setText(_translate("MainWindow", "Print...")) 

     self.pushButton_5.clicked.connect(run) 

def run(): 
    openfile = QtGui.QFileDialog.getOpenFileName(self) # Filename line 
    f = open(openfile, 'r') # New line 
    data = f.read() 


if __name__ == "__main__": 
    import sys 
    app = QtWidgets.QApplication(sys.argv) 
    MainWindow = QtWidgets.QMainWindow() 
    ui = Ui_MainWindow() 
    ui.setupUi(MainWindow) 
    MainWindow.show() 
    sys.exit(app.exec_()) 
+0

파이썬은 일반적으로 오류 메시지가 표시됩니다. 이게 뭐야? – Boiethios

+0

''''''''''''''' 함수에'self'가 없다면 클래스 멤버가되거나 자신을 사용하지 말아야합니다. – Fabio

+0

질문에 그 정보를 추가하십시오. 매우 중요합니다. 그리고 함수의 전체 코드 ('def' 라인 포함)를 넣으십시오. – Boiethios

답변

1

.

def run(self): 
    openfile = QtWidgets.QFileDialog.getOpenFileName() # Filename line 
    f = open(openfile[0], 'r') # New line 
    data = f.read() 

를와 연결 : : 그냥 클래스에 기능을 넣어

self.pushButton_5.cicked.connect(self.run) 
+0

그래도 작동하지 않습니다. 그것은 PyQt5에서'QtWidgets.QFileDialog'이어야합니다. 또한 반환 값은 튜플이므로 다음 줄도 실패합니다. 그것은'open (openfile [0], 'r')'이어야합니다. – ekhumoro

+0

@ekhumoro 귀하의 답변을 게시 할 수 있습니까? 나는 pyqt를 잘 모른다. – Boiethios

+0

코드 예제를 수정했습니다. – ekhumoro