QtWidgets.QFileDialog.getOpenFileName returns a Tuple

tisaconundrum picture tisaconundrum · Apr 20, 2017 · Viewed 9k times · Source

I've recently updated to the new version of Qt5 for Python. In doing so, I've been having to alter my code in accordance to some of the notable changes that have occurred. I wanted to get some insight into this line of code that I've created. It feels like a dirty way of solving the problem of getting a string instead of a tuple from the function. (Note the [0] at the end of the line)

filename = QtWidgets.QFileDialog.getOpenFileName(None, "Open " + key + " Data File", '.', "(*.csv)")[0]

I want filename = {str}'C:/.././.../format.csv'

not filename = <class 'tuple'>: ('C:/.././.../format.csv', '(*.csv)')

Answer

tisaconundrum picture tisaconundrum · Apr 20, 2017

After looking through a repository that was being updated to Qt5 as well; I found they used a line like this.

filename, _filter = QtWidgets.QFileDialog.getOpenFileName(None, "Open " + key + " Data File", '.', "(*.csv)")

filename is a string and the (*.csv) gets discarded in the _filter variable