TypeError: unbound method QFileDialog.selectedFiles() needs an argument
-
I'm trying to make a file opening system using a QFileDialog, but when I try to get the selected files from it it throws a TypeError, despite the fact that nowhere in any documentation does this method seem to take an argument.
Here is the relevant portion of my code:def openFile(): getFile = QtWidgets.QFileDialog getFile.getOpenFileName(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp;;Project Files (*.ifx)") openedFile = getFile.selectedFiles() # This is the code that throws the error def menuTriggers(button): if button.objectName() == "actionOpen": openFile() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) window = uiLoader.load(mainWindow) help = QtWidgets.QMenuBar window.show() help = window.menubar.triggered.connect(menuTriggers) sys.exit(app.exec())I tried using getFile variable as the argument, however that returned the error
TypeError: descriptor 'selectedFiles' for 'PySide6.QtWidgets.QFileDialog' objects doesn't apply to a 'Shiboken.ObjectType' object
I also noticed that png files do not show up in the file dialog despite being in my code
This code uses PySide6 -
Hi and welcome to devnet,
getFile = QtWidgets.QFileDialog # Parenthesis are missing hereYour
getFileobject is not an instance ofQFileDialogas you are missing the parenthesis. In this case, it's just an other name forQFileDialog.@SGaist said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:
getFile = QtWidgets.QFileDialog # Parenthesis are missing hereYour getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.
I am not sure my esteemed colleague, @SGaist, looked closely enough at exactly what you have written :)
@SamuraiDestroy
You were not necessarily missing parentheses ongetFile = QtWidgets.QFileDialog--- it depends which subsequent line you look at. It is correct for thegetFile.getOpenFileName()statement but wrong for thegetFile.selectedFiles()one.You have a problem here with your attempted code.
QtWidgets.QFileDialog.getOpenFileName()is a static method (see the docs) whileQtWidgets.QFileDialog.selectedFiles()is an instance method (see the docs). This means you cannot use them together, they do not work on the same object.selectedFiles()will never look at or see what has happened in thegetOpenFileName()call.Are you wanting the user to open a single file or multiple files? If we assume single file then you do not need
selectedFiles()at all and can use the simple static method:(openedFile, filter) = QtWidgets.QFileDialog.getOpenFileName(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp;;Project Files (*.ifx)")The static
getOpenFileName()puts up the dialog, waits for the user to select and press a button, and returns the (single) file selected (I think the empty string if the user presses Cancel, so you should check for that). Note that the the way the PythongetOpenFileName()works, unlike the C++ one, is that it returns a two element list: the first element is the file selected and the second element is the filter chosen by the user, which you are rarely interested in. [Btw yourImage filesstring clearly looks wrong --- count the parentheses --- so that may be interfering with its filter behaviour.]If you really want more than that, such as the ability to return multiple files selected, you cannot use one of the static methods, you need to use an instance method to do the whole lot. Something like:
dialog = QtWidgets.QFileDialog() dialog.setWindowTitle("Open Image") dialog.setDirectory("~") # if "~" works, else path to home directory dialog.setFilter("Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)") dialog.setFileMode(QtWidgets.QFileDialog.ExistingFiles) openedFiles = [] if dialog.exec(): openedFiles = dialog.selectedFiles()More complicated, and the sort of thing which the static
getOpenFileName()does internally.I suspect you do not want multiple files selected and need only the static code per the first example.
P.S.
I just noticed that even if you do want multiple files selected there is actually a static function for that too:(openedFiles, filter) = QtWidgets.QFileDialog.getOpenFileNames(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)")Notice
getOpenFileNames()plural. So you could use that instead of the instance method. But I have left the instance stuff in, it can be useful in various circumstances where static methods do not do quite do what you want. -
Hi and welcome to devnet,
getFile = QtWidgets.QFileDialog # Parenthesis are missing hereYour
getFileobject is not an instance ofQFileDialogas you are missing the parenthesis. In this case, it's just an other name forQFileDialog. -
Hi and welcome to devnet,
getFile = QtWidgets.QFileDialog # Parenthesis are missing hereYour
getFileobject is not an instance ofQFileDialogas you are missing the parenthesis. In this case, it's just an other name forQFileDialog.@SGaist Thank you so much, that fixed the error, however when I print out the openedFile variable, it only prints an empty list regardless of what was selected. The issue of .png files not showing up is still present however.
-
Hi and welcome to devnet,
getFile = QtWidgets.QFileDialog # Parenthesis are missing hereYour
getFileobject is not an instance ofQFileDialogas you are missing the parenthesis. In this case, it's just an other name forQFileDialog.@SGaist said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:
getFile = QtWidgets.QFileDialog # Parenthesis are missing hereYour getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.
I am not sure my esteemed colleague, @SGaist, looked closely enough at exactly what you have written :)
@SamuraiDestroy
You were not necessarily missing parentheses ongetFile = QtWidgets.QFileDialog--- it depends which subsequent line you look at. It is correct for thegetFile.getOpenFileName()statement but wrong for thegetFile.selectedFiles()one.You have a problem here with your attempted code.
QtWidgets.QFileDialog.getOpenFileName()is a static method (see the docs) whileQtWidgets.QFileDialog.selectedFiles()is an instance method (see the docs). This means you cannot use them together, they do not work on the same object.selectedFiles()will never look at or see what has happened in thegetOpenFileName()call.Are you wanting the user to open a single file or multiple files? If we assume single file then you do not need
selectedFiles()at all and can use the simple static method:(openedFile, filter) = QtWidgets.QFileDialog.getOpenFileName(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp;;Project Files (*.ifx)")The static
getOpenFileName()puts up the dialog, waits for the user to select and press a button, and returns the (single) file selected (I think the empty string if the user presses Cancel, so you should check for that). Note that the the way the PythongetOpenFileName()works, unlike the C++ one, is that it returns a two element list: the first element is the file selected and the second element is the filter chosen by the user, which you are rarely interested in. [Btw yourImage filesstring clearly looks wrong --- count the parentheses --- so that may be interfering with its filter behaviour.]If you really want more than that, such as the ability to return multiple files selected, you cannot use one of the static methods, you need to use an instance method to do the whole lot. Something like:
dialog = QtWidgets.QFileDialog() dialog.setWindowTitle("Open Image") dialog.setDirectory("~") # if "~" works, else path to home directory dialog.setFilter("Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)") dialog.setFileMode(QtWidgets.QFileDialog.ExistingFiles) openedFiles = [] if dialog.exec(): openedFiles = dialog.selectedFiles()More complicated, and the sort of thing which the static
getOpenFileName()does internally.I suspect you do not want multiple files selected and need only the static code per the first example.
P.S.
I just noticed that even if you do want multiple files selected there is actually a static function for that too:(openedFiles, filter) = QtWidgets.QFileDialog.getOpenFileNames(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)")Notice
getOpenFileNames()plural. So you could use that instead of the instance method. But I have left the instance stuff in, it can be useful in various circumstances where static methods do not do quite do what you want. -
@SGaist said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:
getFile = QtWidgets.QFileDialog # Parenthesis are missing hereYour getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.
I am not sure my esteemed colleague, @SGaist, looked closely enough at exactly what you have written :)
@SamuraiDestroy
You were not necessarily missing parentheses ongetFile = QtWidgets.QFileDialog--- it depends which subsequent line you look at. It is correct for thegetFile.getOpenFileName()statement but wrong for thegetFile.selectedFiles()one.You have a problem here with your attempted code.
QtWidgets.QFileDialog.getOpenFileName()is a static method (see the docs) whileQtWidgets.QFileDialog.selectedFiles()is an instance method (see the docs). This means you cannot use them together, they do not work on the same object.selectedFiles()will never look at or see what has happened in thegetOpenFileName()call.Are you wanting the user to open a single file or multiple files? If we assume single file then you do not need
selectedFiles()at all and can use the simple static method:(openedFile, filter) = QtWidgets.QFileDialog.getOpenFileName(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp;;Project Files (*.ifx)")The static
getOpenFileName()puts up the dialog, waits for the user to select and press a button, and returns the (single) file selected (I think the empty string if the user presses Cancel, so you should check for that). Note that the the way the PythongetOpenFileName()works, unlike the C++ one, is that it returns a two element list: the first element is the file selected and the second element is the filter chosen by the user, which you are rarely interested in. [Btw yourImage filesstring clearly looks wrong --- count the parentheses --- so that may be interfering with its filter behaviour.]If you really want more than that, such as the ability to return multiple files selected, you cannot use one of the static methods, you need to use an instance method to do the whole lot. Something like:
dialog = QtWidgets.QFileDialog() dialog.setWindowTitle("Open Image") dialog.setDirectory("~") # if "~" works, else path to home directory dialog.setFilter("Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)") dialog.setFileMode(QtWidgets.QFileDialog.ExistingFiles) openedFiles = [] if dialog.exec(): openedFiles = dialog.selectedFiles()More complicated, and the sort of thing which the static
getOpenFileName()does internally.I suspect you do not want multiple files selected and need only the static code per the first example.
P.S.
I just noticed that even if you do want multiple files selected there is actually a static function for that too:(openedFiles, filter) = QtWidgets.QFileDialog.getOpenFileNames(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)")Notice
getOpenFileNames()plural. So you could use that instead of the instance method. But I have left the instance stuff in, it can be useful in various circumstances where static methods do not do quite do what you want.@JonB said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:
@SGaist said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:
getFile = QtWidgets.QFileDialog # Parenthesis are missing hereYour getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.
I am not sure my esteemed colleague, @SGaist, looked closely enough at exactly what you have written :)
I did but was way too short on my answer :-)
I somehow forgot to add the static function bit although I had it mind. -
@SGaist said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:
getFile = QtWidgets.QFileDialog # Parenthesis are missing hereYour getFile object is not an instance of QFileDialog as you are missing the parenthesis. In this case, it's just an other name for QFileDialog.
I am not sure my esteemed colleague, @SGaist, looked closely enough at exactly what you have written :)
@SamuraiDestroy
You were not necessarily missing parentheses ongetFile = QtWidgets.QFileDialog--- it depends which subsequent line you look at. It is correct for thegetFile.getOpenFileName()statement but wrong for thegetFile.selectedFiles()one.You have a problem here with your attempted code.
QtWidgets.QFileDialog.getOpenFileName()is a static method (see the docs) whileQtWidgets.QFileDialog.selectedFiles()is an instance method (see the docs). This means you cannot use them together, they do not work on the same object.selectedFiles()will never look at or see what has happened in thegetOpenFileName()call.Are you wanting the user to open a single file or multiple files? If we assume single file then you do not need
selectedFiles()at all and can use the simple static method:(openedFile, filter) = QtWidgets.QFileDialog.getOpenFileName(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp;;Project Files (*.ifx)")The static
getOpenFileName()puts up the dialog, waits for the user to select and press a button, and returns the (single) file selected (I think the empty string if the user presses Cancel, so you should check for that). Note that the the way the PythongetOpenFileName()works, unlike the C++ one, is that it returns a two element list: the first element is the file selected and the second element is the filter chosen by the user, which you are rarely interested in. [Btw yourImage filesstring clearly looks wrong --- count the parentheses --- so that may be interfering with its filter behaviour.]If you really want more than that, such as the ability to return multiple files selected, you cannot use one of the static methods, you need to use an instance method to do the whole lot. Something like:
dialog = QtWidgets.QFileDialog() dialog.setWindowTitle("Open Image") dialog.setDirectory("~") # if "~" works, else path to home directory dialog.setFilter("Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)") dialog.setFileMode(QtWidgets.QFileDialog.ExistingFiles) openedFiles = [] if dialog.exec(): openedFiles = dialog.selectedFiles()More complicated, and the sort of thing which the static
getOpenFileName()does internally.I suspect you do not want multiple files selected and need only the static code per the first example.
P.S.
I just noticed that even if you do want multiple files selected there is actually a static function for that too:(openedFiles, filter) = QtWidgets.QFileDialog.getOpenFileNames(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)")Notice
getOpenFileNames()plural. So you could use that instead of the instance method. But I have left the instance stuff in, it can be useful in various circumstances where static methods do not do quite do what you want.@JonB said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:
[Btw your Image files string clearly looks wrong --- count the parentheses --- so that may be interfering with its filter behaviour.]
Ah, I somehow missed that before. Yep, adding the extra ) fixed it.@JonB said in TypeError: unbound method QFileDialog.selectedFiles() needs an argument:
I just noticed that even if you do want multiple files selected there is actually a static function for that too:
This works perfectly and does exactly what I was trying to do, thank you
-
S SamuraiDestroy has marked this topic as solved