QFileDialog closes window when called

2019-07-10 03:28发布

问题:

This if my first time asking a question here so please forgive me for any mistakes.

So my problem is when I run my program it works fine. But as soon as I click Search the program closes.

I use self.pushButton_5.cicked.connect(run) to call on a function that runs:

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

and this is what causes my program to close. If I replace this with print('Hello') it works fine.

I am using Qt Creator to make the GUI and modifying it using Pycharm. I'm Running PyQt5 on windows 7 using Python 3.5. I'm very new to this (4-5 hours tops) however I have been using python in school for the full year (still very basic but I have an okay understanding). This if for a class project that makes up a large portion of the end of year marks.

My full code is below:

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

回答1:

You are using the keyword self from a non-member function. Just put the function in your class:

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

an connect it with:

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