Как сделать так, чтобы при нажатии кнопки запускалась определенная песня? (PyQt5)

Пишу плеер. Столкнулся с проблемой, что при нажатии определенной кнопки не могу запустить определенную песню. Из всех треков запускается именно последний. Как я могу, чтобы при нажатии, например, кнопки, показанной стрелочкой на картинке, запускалась именно эта песня?

введите сюда описание изображения

from PyQt5.Qt import *
import sys
import glob
import mp3_tagger
import mutagen
from mutagen.mp3 import MP3


#CSS
slider1 = """

QSlider::handle {
    height: 10px;
    background: yellow;
    margin: 0 -4px; 
}

QSlider::add-page {
    background: transparent;
}

QSlider::sub-page {
    background: yellow;
}

"""

slider2 = """

QSlider::handle {
    height: 10px;
    background: green;
    margin: 0 -4px; 
}

QSlider::add-page {
    background: grey;
}

QSlider::sub-page {
    background: green;
}

"""
class MainWindow(QWidget):
    def __init__(self, parent = None):
        QWidget.__init__(self, parent)

        self.setMinimumSize(1000,600)
        self.setWindowFlags(Qt.CustomizeWindowHint)

        #Создание компонентов и контейнеров
        self.hbox_1 = QHBoxLayout()
        self.hbox_2 = QHBoxLayout()
        self.hbox_2.setSpacing(10)
        self.hbox_3 = QHBoxLayout()
        self.vbox_1 = QVBoxLayout()
        self.vbox_1.setSpacing(40)
        self.vbox_1.setContentsMargins(0,15,0,100)
        self.vbox_2 = QVBoxLayout()
        self.vbox_2.setSpacing(0)
        self.vbox_3 = QVBoxLayout()

        self.menu_button = QPushButton()
        self.app_button = QPushButton()

        self.current_track_button_1 = QPushButton()
        self.current_track_button_2 = QPushButton()
        self.hide_button = QPushButton()
        self.maximinimize_button = QPushButton()
        self.close_button = QPushButton()
        self.prev_track_button = QPushButton()
        self.start_pause_button = QPushButton()
        self.next_track_button = QPushButton()
        self.repeat_track_button = QPushButton()
        self.mix_tracks_button = QPushButton()
        self.info_button = QPushButton()
        self.tracks_button = QPushButton()
        self.folder_button = QPushButton()
        self.switch_theme_button = QPushButton()

        self.app_label = QLabel("MP3-Player")

        self.sound = QSlider(Qt.Horizontal)
        self.duration = QSlider(Qt.Horizontal)

        self.stacked_wt = QStackedWidget()

        self.package = QFileDialog(self)

        self.table = QTableWidget()

        #Добавление компонентов в контейнеры и прочие подготовки
        self.hbox_1.addWidget(self.menu_button)
        self.hbox_1.addWidget(self.app_button)
        self.hbox_1.addWidget(self.app_label)
        self.hbox_1.addStretch(0)
        self.hbox_1.addWidget(self.hide_button)
        self.hbox_1.addWidget(self.maximinimize_button)
        self.hbox_1.addWidget(self.close_button)

        self.hbox_2.addWidget(self.current_track_button_1)
        self.hbox_2.addWidget(self.current_track_button_2)
        self.hbox_2.addStretch(0)
        self.hbox_2.addWidget(self.prev_track_button)
        self.hbox_2.addWidget(self.start_pause_button)
        self.hbox_2.addWidget(self.next_track_button)
        self.hbox_2.addWidget(self.repeat_track_button)
        self.hbox_2.addWidget(self.mix_tracks_button)
        self.hbox_2.addWidget(self.sound)

        self.vbox_1.addWidget(self.tracks_button)
        self.vbox_1.addWidget(self.info_button)
        self.vbox_1.addWidget(self.folder_button)
        self.vbox_1.addStretch(0)
        self.vbox_1.addWidget(self.switch_theme_button)

        self.vbox_2.addWidget(self.stacked_wt)
        self.vbox_2.addWidget(self.duration)
        self.vbox_2.addLayout(self.hbox_2)

        self.hbox_3.addLayout(self.vbox_1)
        self.hbox_3.addLayout(self.vbox_2)

        self.vbox_3.addLayout(self.hbox_1)
        self.vbox_3.addLayout(self.hbox_2)
        self.vbox_3.addLayout(self.hbox_3)

        self.setLayout(self.vbox_3)
        self.setStyleSheet("background-color: rgb(27,27,35)")

        self.table.setColumnCount(5)
        self.table.setHorizontalHeaderLabels(['Значок', 'Исполнитель', 'Название', 'Длительность', 'Воспроизведение'])
        self.table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

        self.stacked_wt.setStyleSheet("background-color: rgb(39,43,55)")
        self.stacked_wt.addWidget(self.table)

        self.duration.setStyleSheet(slider1)
        self.duration.setFixedHeight(6)
        self.duration.setPageStep(2)

        self.sound.setStyleSheet(slider2)
        self.sound.setFixedHeight(20)
        self.sound.setPageStep(2)

        self.app_label.setStyleSheet("color: rgb(255,255,255)")

        self.switch_theme_button.setStyleSheet("background-color: transparent")
        effect = QGraphicsBlurEffect()
        effect.setBlurRadius(1)
        self.switch_theme_button.setGraphicsEffect(effect)
        self.switch_theme_button.setIcon(QIcon("imgs/moon.png"))
        self.switch_theme_button.setIconSize(QSize(50,50))

        self.app_button.setIcon(QIcon("imgs/app_ico.png"))
        self.app_button.setStyleSheet("background-color: transparent")

        self.menu_button.setIcon(QIcon("imgs/menu.png"))
        self.menu_button.setIconSize(QSize(20,20))
        self.menu_button.setStyleSheet("background-color: transparent")

        self.folder_button.setIcon(QIcon("imgs/folder.png"))
        self.folder_button.setText("  Хранилище")
        self.folder_button.setIconSize(QSize(20, 20))
        self.folder_button.setStyleSheet("QPushButton:!hover {background-color: transparent; color: rgb(149,149,157)} \
                                        QPushButton:hover {background-color: rgb(39,43,55); border-radius: 5px; color: rgb(149,149,157)}")

        self.info_button.setIcon(QIcon("imgs/info.png"))
        self.info_button.setText("  Информация")
        self.info_button.setIconSize(QSize(20, 20))
        self.info_button.setStyleSheet("QPushButton:!hover {background-color: transparent; color: rgb(149,149,157)} \
                                        QPushButton:hover {background-color: rgb(39,43,55); border-radius: 5px; color: rgb(149,149,157)}")

        self.tracks_button.setIcon(QIcon("imgs/vinyl.png"))
        self.tracks_button.setText("  Треки")
        self.tracks_button.setIconSize(QSize(20, 20))
        self.tracks_button.setStyleSheet("QPushButton:!hover {background-color: transparent; color: rgb(149,149,157)} \
                                          QPushButton:hover {background-color: rgb(39,43,55); border-radius: 5px; color: rgb(149,149,157)}")

        self.hide_button.setIcon(QIcon("imgs/minimize.png"))
        self.hide_button.setFixedSize(QSize(20, 20))
        self.hide_button.setIconSize(QSize(20, 20))
        self.hide_button.setStyleSheet("QPushButton:!hover {background-color: transparent} \
                                        QPushButton:hover {background-color: rgb(39,43,55); border-radius: 5px}")

        self.maximinimize_button.setIcon(QIcon("imgs/maximize.png"))
        self.maximinimize_button.setFixedSize(QSize(20,20))
        self.maximinimize_button.setIconSize(QSize(25,25))
        self.maximinimize_button.setStyleSheet("QPushButton:!hover {background-color: transparent} \
                                                QPushButton:hover {background-color: rgb(39,43,55); border-radius: 5px}")

        self.close_button.setIcon(QIcon("imgs/close.png"))
        self.close_button.setStyleSheet("QPushButton:!hover {background-color: transparent} \
                                         QPushButton:hover {background-color: rgb(39,43,55); border-radius: 5px}")
        self.close_button.setFixedSize(QSize(20, 20))
        self.close_button.setIconSize(QSize(15, 15))

        self.start_pause_button.setIcon(QIcon("imgs/start.png"))
        self.start_pause_button.setFixedSize(QSize(30, 30))
        self.start_pause_button.setIconSize(QSize(30,30))
        self.start_pause_button.setStyleSheet("QPushButton:!hover {background-color: transparent} \
                                               QPushButton:hover {background-color: rgb(39,43,55); border-radius: 5px}")

        self.prev_track_button.setIcon(QIcon("imgs/previous.png"))
        self.prev_track_button.setFixedSize(QSize(30, 30))
        self.prev_track_button.setIconSize(QSize(30, 30))
        self.prev_track_button.setStyleSheet("QPushButton:!hover {background-color: transparent} \
                                              QPushButton:hover {background-color: rgb(39,43,55); border-radius: 5px}")

        self.next_track_button.setIcon(QIcon("imgs/next.png"))
        self.next_track_button.setFixedSize(QSize(30,30))
        self.next_track_button.setIconSize(QSize(45, 45))
        self.next_track_button.setStyleSheet("QPushButton:!hover {background-color: transparent} \
                                              QPushButton:hover {background-color: rgb(39,43,55); border-radius: 5px}")

        self.repeat_track_button.setIcon(QIcon("imgs/one.png"))
        self.repeat_track_button.setFixedSize(QSize(30, 30))
        self.repeat_track_button.setIconSize(QSize(30, 30))
        self.repeat_track_button.setStyleSheet("QPushButton:!hover {background-color: transparent} \
                                                      QPushButton:hover {background-color: rgb(39,43,55); border-radius: 5px}")

        self.mix_tracks_button.setIcon(QIcon("imgs/mix.png"))
        self.mix_tracks_button.setFixedSize(QSize(30, 30))
        self.mix_tracks_button.setIconSize(QSize(30, 30))
        self.mix_tracks_button.setStyleSheet("QPushButton:!hover {background-color: transparent} \
                                                             QPushButton:hover {background-color: rgb(39,43,55); border-radius: 5px}")
        self.hide_button.clicked.connect(self.hide)
        self.maximinimize_button.clicked.connect(self.maximinimize)
        self.close_button.clicked.connect(self.close_app)

        self.folder_button.clicked.connect(self.choose_folder)

    def hide(self):
        self.showMinimized()

    def maximinimize(self):
        if self.isMaximized():
            self.showNormal()
        elif self.size() == (1000,600):
            self.showMaximized()
        else:
            self.showMaximized()

    def close_app(self):
        self.close()

    def choose_folder(self):
        self.name_of_folder = QUrl(self.package.getExistingDirectoryUrl()).toString()[8:]
        if self.name_of_folder.__len__() != 0:
            self.table.setRowCount(0)

        self.songs_list = glob.glob(f"{self.name_of_folder}/*.mp3")

        for d in self.songs_list:
            self.name = QUrl(f"file:///{d}")
            mp3 = mp3_tagger.MP3File(f"{d}")
            tags = mp3.get_tags()
            song = "Не определено"
            artist = "Не определено"
            pixmap = QIcon("imgs/no_icon.png")
            f = MP3(f"{d}")
            duration = int(f.info.length)
            minutes = duration//60
            secs = duration%60
            duration = f"{minutes}" + ":" + f"{secs}"

            try:
                song = tags["ID3TagV1"]["song"]
                artist = tags["ID3TagV1"]["artist"]
                pixlist = self.saveMP3pix(self.name)
                pixmap = QIcon(pixlist[0])

            except:
                pass

            row_pos = self.table.rowCount()
            self.table.insertRow(row_pos)
            button = QPushButton()

            button.setStyleSheet("background-color: transparent")
            button.setFixedSize(30,30)
            button.setIcon(QIcon("imgs/play.png"))
            button.setIconSize(QSize(20,20))
            button_wt = QWidget()
            hbox = QHBoxLayout()
            hbox.setContentsMargins(0,0,0,0)
            hbox.addWidget(button)
            button_wt.setLayout(hbox)

            icon = QPushButton()
            icon.setStyleSheet("background-color: transparent")
            icon.setFixedSize(30, 30)
            icon.setIcon(pixmap)
            icon.setIconSize(QSize(30, 30))
            icon_wt = QWidget()
            hbox_2 = QHBoxLayout()
            hbox_2.setContentsMargins(0, 0, 0, 0)
            hbox_2.addWidget(icon)
            icon_wt.setLayout(hbox_2)

            dur = QLabel(f"{duration}")
            dur.setStyleSheet("background-color: transparent")
            dur_wt = QWidget()
            hbox_3 = QHBoxLayout()
            hbox_3.setContentsMargins(0, 0, 0, 0)
            hbox_3.addStretch(0)
            hbox_3.addWidget(dur)
            hbox_3.addStretch(0)
            dur_wt.setLayout(hbox_3)

            self.table.setCellWidget(row_pos,0,icon_wt)
            self.table.setItem(row_pos, 1, QTableWidgetItem(artist))
            self.table.setItem(row_pos, 2, QTableWidgetItem(song))
            self.table.setCellWidget(row_pos, 3, dur_wt)
            self.table.setCellWidget(row_pos, 4, button_wt)

    def saveMP3pix(self, filename):
        filename = filename.toLocalFile()
        mp3 = mutagen.File(filename)

        allpix = []

        for tag in mp3.items():
            if tag[0][:4] == 'APIC':
                pic = tag[1]
                new_name = f'my_pic_{len(allpix)}.' + str(pic.mime.split('/')[1])
                file = open(new_name, "wb")

                file.write(pic.data)
                file.close()
                allpix.append(new_name)

        return allpix

    def resizeEvent(self, event):
        difference_x = (self.stacked_wt.width() -871)/5
        self.table.setColumnWidth(0, int(174 + difference_x))
        self.table.setColumnWidth(1, int(174+ difference_x))
        self.table.setColumnWidth(2, int(174+ difference_x))
        self.table.setColumnWidth(3, int(174+ difference_x))
        self.table.setColumnWidth(4, int(174+ difference_x))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

Ответы (0 шт):