Мне нужно подключить папку с муз.файлами как базу данных в приложении QT питон 6. я новичок((

Камилико

Новичок
Пользователь
Мар 15, 2025
1
0
1
# This Python file uses the following encoding: utf-8
import sys

from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QListView, QVBoxLayout, QPushButton, QLabel, QFileDialog, QMessageBox

from PySide6.QtGui import QStandardItemModel, QStandardItem

# Important:
# You need to run the following command to generate the ui_form.py file
# pyside6-uic form.ui -o ui_form.py, or
# pyside2-uic form.ui -o ui_form.py

class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.create_database()
self.load_audio_files()

def add_audio_files_from_folder(self):
folder_path = r"\DESKTOP-SULBENRUsers-Desktopmp" # путь

audio_extensions = ('.mp3', '.wav', '.ogg') # Поддерживаемые расширения
added_files = 0

for filename in listdir(folder_path):
if filename.endswith(audio_extensions):
name = filename
full_path = path.join(folder_path, filename)

self.cursor.execute("INSERT INTO audio (name, path) VALUES (?, ?)", (name, full_path))
added_files += 1

self.conn.commit()
QMessageBox.information(self, "Успех", f"Добавлено {added_files} аудиофайлов из папки.")
self.load_audio_files() # Обновляем список аудиофайлов

self.add_button = QPushButton('Добавить аудиофайлы из папки', self)
self.add_button.clicked.connect(self.add_audio_files_from_folder) # Подключаем кнопку к методу
layout.addWidget(self.add_button)

def load_audio_files(self):
self.cursor.execute("SELECT * FROM audio")
self.audio_files = self.cursor.fetchall()

if self.audio_files:
self.label.setText(f'Найдены аудиофайлы: {len(self.audio_files)}')
else:
self.label.setText('Нет доступных аудиофайлов')

def add_audio_files_from_folder(self):
# Открываем диалог выбора папки
folder_path = QFileDialog.getExistingDirectory(self, "Выберите папку с аудиофайлами")

if folder_path:
audio_extensions = ('.mp3', '.wav', '.ogg') # Поддерживаемые расширения
added_files = 0

for filename in listdir(folder_path):
if filename.endswith(audio_extensions):
name = filename
full_path = path.join(folder_path, filename)

self.cursor.execute("INSERT INTO audio (name, path) VALUES (?, ?)", (name, full_path))
added_files += 1

self.conn.commit()
QMessageBox.information(self, "Успех", f"Добавлено {added_files} аудиофайлов из папки.")
self.load_audio_files() # Обновляем список аудиофайлов

def play_audio(self):
if not self.audio_files:
QMessageBox.warning(self, "Ошибка", "Нет доступных аудиофайлов для воспроизведения.")
return

# Воспроизводим первый аудиофайл из списка
audio_file = self.audio_files[0][2] # Получаем путь к файлу
pygame.mixer.init()
pygame.mixer.music.load(audio_file)
pygame.mixer.music.play()

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
ex.show()
sys.exit(app.exec())
 

4olshoy_blen

Популярный
Пользователь
Ноя 13, 2022
464
128
43

Мне нужно подключить папку с муз.файлами как базу данных в приложении QT питон 6. я новичок((​


ну так подключай, кто ж запрещает? Или это сам факт того, что ты новичок, мешает? Так выучи основы, какие проблемы?
 

Форум IT Специалистов