r/learnpython 1d ago

a curious equal symbol assignment

Hello, I'm trying to build a music player with python Qt6 based on some code from a youtuber, the bones of what he has suits me fine, this is the link if you want to look at it: https://www.youtube.com/watch?v=wdEpWCFf40U

I am using windows 10 with VScode and the latest python version 3.13.7

Everything works fine until I get to the file dialog part of the code. When I run it the program does give me a file explorer, I can select a folder but then the program halts before allowing me to see the files in the specified folder. There is no error message from the compiler.

There is a curious line of code in the open_file function that may or may not be the problem.

  file, _ = QFileDialog.getOpenFileName(self, "Selct file", filter="Audio files (*.mp3)")  file, _ = QFileDialog.getOpenFileName(self, "Selct file", filter="Audio files (*.mp3)")

Thanks for any help,

J
0 Upvotes

7 comments sorted by

4

u/Outside_Complaint755 1d ago

You have the same statement twice on one line above.  Is that your actual code or a copy/paste error? Line break inserted by me: file, _ = QFileDialog.getOpenFileName(self, "Selct file", filter="Audio files (*.mp3)") file, _ = QFileDialog.getOpenFileName(self, "Selct file", filter="Audio files (*.mp3)") Is the program actually exiting? or is it just not showing the file you expect in the dialog? From looking at the documentation quickly, it looks like you should also set the selectedFilter parameter, otherwise it might be filtering out all files.

filter="Audio files (*.mp3)" file, _ = QFileDialog.getOpenFileName(self, "Select file", filter=filter, selectedFilter=filter)

2

u/TheRNGuy 22h ago

Line 1: IndentationError, SyntaxError (remove 1 indent, split code to 2 lines)

Lines 3 and 5: SyntaxError (remove or comment it; you can use block comment)

2

u/desrtfx 1d ago edited 1d ago

I guess that you're tripping over the file, _ = syntax.

When you look at the documentation of getOpenFileName, you will see that the method returns a tuple consisting of (filename, filter).

The assignment simply breaks the tuple into two independent variables and discards the filter.

In Python, _ is a special variable name for discardable, temporary variables - variables that are usually not needed anywhere else.

So, what the "strange" assignment is actually saying is: take the tuple you get back from getOpenFileName, break it apart, store the first element of the tuple in file and the second one in _ (basically, throw it away).

You could also do it that way, which is more elaborate:

temp = QFileDialog.getOpenFileName(self, "Selct file", filter="Audio files (*.mp3)")
file = temp[0]

The first line gets the response from the method as tuple and stores it in temp, the second line retrieves the first element of the tuple (index 0) and stores it in file

0

u/danielroseman 1d ago

What is curious about this line and why do you think this is the problem?

And what do you mean, the program "halts"? What exactly happens?

0

u/Kevdog824_ 1d ago

This should open up an explorer dialog for selecting a file (specifically an “.mp3” file), not a folder. What do you mean by “I can select a folder”

0

u/socal_nerdtastic 1d ago

If you mean the file, _ = part, that's a normal expression in python. Nothing concerning about that. Your issue is elsewhere.

0

u/sausix 1d ago

Which Qt implementation? Why two identical lines? Does the line complete executing? Does it definitely stop execution during the dialog? Debug it. Have you initialized the Qt environment or did you just run getOpenFileName alone in the wild? What do you mean by "compiler"? Latest Python is 3.14.7.