I want to create 2 upload file button in PyQt. One for image and one for .csv file. If the uploaded file is not in image or .csv file it should thrown an message.

I have search a lot on internet and in internet but couldn't find answer.

1 Answer 1


If the uploaded file is not in image or .csv file it should thrown an message.

It is not the most common approach I think.

The best way to do this is to create a file dialog, that allows to choose only one extension. Example:

def upload_csv(self):
    dialog = QtGui.QFileDialog()
    fname = dialog.getOpenFileName(None, "Import CSV", "", "CSV data files (*.csv)")

Then just connect this function to any button you want. File dialog appears each time you click on it and you can choose only csv file. You can combine more extensions in one dialog if you want.

  • Thanks for the quick response, could you please explain me how to link function to dialog box in PyQt?
    – user730119
    Aug 28, 2017 at 11:56
  • Could you please show one example, actually I am new to it. Thanks in advance.
    – user730119
    Aug 28, 2017 at 13:59
  • Just create a button and connect this function like that: self.dlg.button.clicked.connect(self.upload_csv) then each time you clikc you'll see a dialog. When you choose a file and click 'ok', file path will be stored in fname variable (3rd line) and you can use it to read this file.
    – dmh126
    Aug 28, 2017 at 15:04
  • Thanks it's working, one more small help can't I through an message if it's closed without selecting a file?
    – user730119
    Aug 28, 2017 at 17:07
  • If you close without selecting a file fname is equal to None. So you can put there a conditional statement if fname: # do something
    – dmh126
    Aug 28, 2017 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.