0

First I created UI file using QT Designer, in that main window I added QTabWidget with 4 tabs. In first tab I added Line edit and pushbutton to select the file, please look below:

enter image description here

Later I converted UI file to python file:

class Ui_MainWindow(object):

def setupUi(self, MainWindow):
    MainWindow.setObjectName("Application")
    MainWindow.setWindowIcon(QtGui.QIcon("ldp.png"))
    MainWindow.resize(1393, 704)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
    self.verticalLayout.setObjectName("verticalLayout")
    self.gridLayout = QtWidgets.QGridLayout()
    self.gridLayout.setObjectName("gridLayout")
    self.widget = QtWidgets.QWidget(self.centralwidget)
    self.widget.setStyleSheet("\n"
    "background-color: rgb(141, 141, 141);")
    self.widget.setObjectName("widget")
    self.gridLayout.addWidget(self.widget, 0, 0, 1, 1)

    #Data_Tab:
    self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
    self.tabWidget.setObjectName("tabWidget")
    self.Data_Tab = QtWidgets.QWidget()
    self.Data_Tab.setObjectName("Data_Tab")

    self.vLayout = QtWidgets.QVBoxLayout(self.Data_Tab)
    self.hLayout = QtWidgets.QHBoxLayout()
    self.Line_Edit = QtWidgets.QLineEdit(self.Data_Tab)
    self.hLayout.addWidget(self.Line_Edit)
    self.select_File = QtWidgets.QPushButton(“Select File”, self.Data_Tab)
    self.hLayout.addWidget(self.select_File)
    self.vLayout.addLayout(self.hLayout)
    self.select_File.clicked.connect(self.loading)

 .....      
  
 def loading(self):
    fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open File", "C:/", "CSV Files 
    (*.csv)");
    self.Line_Edit.setText(fileName)

Above code looks fine, but when I click select file button after running the code. I am not getting the open file window instead application getting closed automatically with out any error.

Even I tried with Mainwindow, object, Ui_Mainwindow instead of passing argument as self. But no use. Could any one please suggest me where I did wrong.

Note: I added only required code here, not complete code.

  • PyQt applications tend to fail silently - in the event that an exception occurs, the program will just close without letting you know where it failed. You may want to look into writing your own exception handler, so that you can determine the cause of the exception(s). Google for things like "pyqt5 sys.excepthook", and that should give you some examples. – Paul M. Jul 05 '20 at 19:38
  • 1) I recommend you to run your script from the console to get the error message, 2) You must provide a [mre] if you want help – eyllanesc Jul 05 '20 at 19:46

1 Answers1

0

Finally I got the answer. Thanks for your suggestions @eyllanesc, @paulM

I did below changes in loading function:

   def loading(self):
        options = QtWidgets.QFileDialog.Options()
        fileName, _ = QtWidgets.QFileDialog.getOpenFileName(None, "Open File", "C:/", 
               "CSV Files (*.csv)", options=options);

Earlier, I did not use file dialog options and also instead of None, passed self. Now, I changed it and my code is working fine.