M
M
Maxim0452020-12-31 13:25:42
Python
Maxim045, 2020-12-31 13:25:42

Why does Python code fail?

I have a code that displays in a list the MP3 files that are in the selected folder. I wanted to add the ability to play the file, the path to which was registered in the code itself. If you click on the button, the file will play, but the VS Code terminal will start spamming the same message: "QCoreApplication::exec: The event loop is already running". If you wait a little longer, a message will appear that the application is not responding, the screen will be lower. I tried to run separately the code responsible for playing the file, no errors occurred. Please tell me where I might have made a mistake
5e0b219c97c62335302231.png

import sys, os, pygame, mutagen.mp3, design
from PyQt5 import QtWidgets

class Browse(QtWidgets.QMainWindow, design.Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.ButtonBrowse.clicked.connect(self.browse_folder)
        self.ButtonPlay.clicked.connect(self.play_track)

    def browse_folder(self):
        self.listWidget.clear()
        directory = QtWidgets.QFileDialog.getExistingDirectory(self, "Выберите папку")

        if directory:
            for file_name in os.listdir(directory):
                if file_name.endswith(".mp3") | file_name.endswith(".wav"):
                    self.listWidget.addItem(file_name)

    def play_track(self):
        song_file = "C:\\Users\\101ap\\Desktop\\Player\\music\\LINKIN PARK\\Meteora\\13. Numb.mp3"

        mp3 = mutagen.mp3.MP3(song_file)
        pygame.mixer.init(frequency=mp3.info.sample_rate)

        pygame.mixer.music.load(song_file)
        pygame.mixer.music.play()

        input()

def main():
    app = QtWidgets.QApplication(sys.argv)
    window = Browse()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex F, 2020-12-31
@delvin-fil

Didn't repeat.
Writes that it is already running. Your loop is most likely wrong. Well, or the python did not close after the previous launch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question