PyQt5 считывание mp3
Я не понимаю работу listwidget, мне нужно что бы при нажатии на элемент списка, в котором выводятся возможные mp3 в директории, срабатывала функция, которая проиграет музыку (например через pyglet or pygame-mixer)
У меня никак не получается реализовать mp3 плеер.
Весь код на pastebin - https://pastebin.com/PzFrRWCa
Часть кода с элементами и функцией.
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
__sortingEnabled = self.listWidget.isSortingEnabled()
self.listWidget.setSortingEnabled(False)
item=self.listWidget.item(0)
item.setText(_translate("MainWindow", "New Item")) #Элементы, пришло в голову что можно воспользоваться такой структурой
item = self.listWidget.item(1) #Но я всё равно не знаю как привязать элемент к файлу и как сделать открытие файла.
item.setText(_translate("MainWindow", "New Item"))
item = self.listWidget.item(2)
item.setText(_translate("MainWindow", "New Item"))
item = self.listWidget.item(3)
item.setText(_translate("MainWindow", 'w'))
self.listWidget.setSortingEnabled(__sortingEnabled)
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.itemlist = ['Item 1', 'Item 2', 'Item 3', 'Item 4', ] #список из элементов
self.listWidget.addItems(self.itemlist)
self.listWidget.itemClicked.connect(self.onClicked)
def onClicked(self, item): #При нажатии на элемент срабатывает функция и выводит "0"
print('0')```
Ответы (1 шт):
Автор решения: Tehnorobot
→ Ссылка
Обновил класс
MainWindowИмпортировал библиотеку
pygameРеализовал плеер
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushbutton_stop = QPushButton('Стоп', self)
self.pushbutton_pause = QPushButton('Пауза', self)
self.pushbutton_remove = QPushButton('Продолжить', self)
self.itemlist = ['Item 1', 'Item 2', 'Item 3', 'Item 4', ] #список из элементов
self.listWidget.addItems(self.itemlist)
self.pushbutton_stop.clicked.connect(self.clickstop)
self.pushbutton_stop.setStyleSheet('QPushButton {color: red;}')
self.pushbutton_stop.move(150,0)
self.pushbutton_pause.clicked.connect(self.clickpause)
self.pushbutton_pause.setStyleSheet('QPushButton {color: yellow;}')
self.pushbutton_pause.move(250,0)
self.pushbutton_remove.clicked.connect(self.clickremove)
self.pushbutton_remove.setStyleSheet('QPushButton {color: green;}')
self.pushbutton_remove.move(250, 0)
self.pushbutton_stop.hide()
self.pushbutton_remove.hide()
self.pushbutton_pause.hide()
self.listWidget.itemClicked.connect(self.onClicked)
def onClicked(self, item):
try:
self.pushbutton_stop.show()
self.pushbutton_pause.show()
pygame.init()
name = item.text() + '.mp3'
pygame.mixer.music.load(name)
pygame.mixer.music.play(1)
except Exception:
self.pushbutton_stop.hide()
self.pushbutton_pause.hide()
self.pushbutton_remove.hide()
print('Error file')
def clickstop(self):
try:
self.pushbutton_stop.hide()
self.pushbutton_pause.hide()
self.pushbutton_remove.hide()
pygame.mixer.music.stop()
pygame.quit()
except Exception:
pass
def clickpause(self):
try:
self.pushbutton_pause.hide()
self.pushbutton_remove.show()
pygame.mixer.music.pause()
except Exception:
pass
def clickremove(self):
try:
self.pushbutton_pause.show()
self.pushbutton_remove.hide()
pygame.mixer.music.unpause()
except Exception:
pass
def closeEvent(self, event):
try:
pygame.mixer.music.stop()
pygame.quit()
except Exception:
pass
finally:
pygame.quit()