当前位置:网站首页>音乐播放器(未完成版本)

音乐播放器(未完成版本)

2022-08-10 22:07:00 出其东门225

 

import pymysql


class Mydb:
    config = {
        "host": "localhost",
        "user": "root",
        "password": "root",
        "db": "music_project",
        "charset": "utf8"
    }

    def __init__(self):
        self.connection = pymysql.connect(**Mydb.config)
        self.cursor = self.connection.cursor()

    def close(self):
        if self.cursor:
            self.cursor.close()
        if self.connection:
            self.connection.close()

    # 插入 修改  删除调用
    def exeDML(self, sql, *args):
        try:
            # 执行sql
            count = self.cursor.execute(sql, args)
            id = self.connection.insert_id()
            # 提交事务
            self.connection.commit()
            return id
        except Exception as e:
            print(e)
            if self.connection:
                self.connection.rollback()
        finally:
            self.close()

    def query_one(self, sql, *args):
        try:
            # 执行sql
            self.cursor.execute(sql, args)
            # 获取结果集
            return self.cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            self.close()

    def query_all(self, sql, *args):
        try:
            # 执行sql
            self.cursor.execute(sql, args)
            # 获取结果集
            return self.cursor.fetchall()
        except Exception as e:
            print(e)
        finally:
            self.close()


if __name__ == "__main__":
    dbutil = Mydb()
    # sql = "insert into emp (empno,ename,sal) values(%s,%s,%s)"
    # count = dbutil.exeDML(sql,9999,'lili',12000)
    # print(count)

    # sql = "select * from emp where empno=%s"
    # emp = dbutil.query_one(sql,7788)
    # print(emp)

    sql = "select * from emp"
    emps = dbutil.query_all(sql)
    for e in emps:
        print(e, end="\n")

from dbUtil import Mydb
import pygame


class MyService:
    def __init__(self):
        self.user = None

    def login(self, uname, password):
        sql = "select * from t_user where uname=%s and password=%s"
        user = Mydb().query_one(sql, uname, password)
        if user:
            self.user = user
            return True
        else:
            return False

    def add_music(self, files):
        for f in files:
            start = f.rfind("/") + 1
            end = f.rfind(".mp3")
            music_name = f[start:end]
            # 根据歌名查询数据是否有该首歌
            sql = "select * from t_music where music_name=%s"
            music = Mydb().query_one(sql, music_name)
            if music:
                # 查询关联表t_list 该用户是否添加了这首歌
                sql = "select * from t_list where mid=%s and uid=%s"
                t_list = Mydb().query_one(sql, music[0], self.user[0])
                if not t_list:
                    sql = "insert into t_list(mid,uid) values(%s,%s)"
                    Mydb().exeDML(sql, music[0], self.user[0])
            else:
                # 将音乐保存到t_music
                sql = "insert into t_music(music_name,path) values(%s,%s)"
                mid = Mydb().exeDML(sql, music_name, f)

                # 用户选择的音乐保存到t_list
                sql = "insert into t_list(mid,uid) values(%s,%s)"
                Mydb().exeDML(sql, mid, self.user[0])

    # 查询用户的音乐列表
    def findListByUser(self):
        sql = "select m.music_name from t_music m,t_list t where m.id=t.mid and t.uid=%s"
        return Mydb().query_all(sql, self.user[0])

    # 删除音乐
    def delete_music(self, music_name):
        # 根据音乐名称查询音乐id
        sql = "select id from t_music where music_name = %s"
        mid = Mydb().query_one(sql, music_name)
        # 删除关联表中的数据及t_list
        sql = "delete from t_list where uid=%s and mid=%s"
        Mydb().exeDML(sql, self.user[0], mid[0])

    # 播放音乐
    def playMuisc(self, music_name):
        # 根据名称查询音乐的path
        sql = "select path from t_music where music_name=%s"
        path = Mydb().query_one(sql, music_name)
        pygame.mixer.init()
        pygame.mixer.music.load(path[0])
        pygame.mixer.music.play()











from MyService import MyService
import tkinter
from tkinter.filedialog import askopenfilenames


class PlayWindow:
    def __init__(self, myService):
        self.myService = myService
        self.listbox = None

    # 导入音乐
    def impMusic(self, even):
        print('点击了导入音乐按钮')
        # 打开磁盘选择音乐
        files = askopenfilenames(filetype=(["mp3", "*.mp3"],))
        # 导入音乐,将选择的音乐保存到数据库
        self.myService.add_music(files)
        self.flush_list()

    # 查询当前用户的音乐列表
    def flush_list(self):
        # 清空列表
        self.listbox.delete(0, tkinter.END)
        # 查询该用户的所有音乐列表
        music_list = self.myService.findListByUser()
        if music_list:
            for m in music_list:
                self.listbox.insert(tkinter.END, m)

    # 播放音乐
    def playMusic(self, even):
        print('点击了播放音乐按钮')
        # 获取播放的音乐
        index = self.listbox.curselection()
        music_name = self.listbox.get(index)
        # 调用方法播放音乐
        self.myService.playMuisc(music_name)

    # 删除音乐
    def deleteMusic(self, even):
        print('点击了删除音乐按钮')
        # 获取删除音乐的名称
        index = self.listbox.curselection()
        music_name = self.listbox.get(index)
        print(music_name)
        # 调用删除方法删除音乐
        self.myService.delete_music(music_name)
        # 刷新列表
        self.flush_list()

    def showWindow(self):
        # 显示窗口
        top = tkinter.Tk()

        # 添加按钮
        play_button = tkinter.Button(top, text="播放")
        imp_button = tkinter.Button(top, text="导入音乐")
        delete_button = tkinter.Button(top, text="删除")
        play_button.grid(row=0, column=0, padx=5, pady=5)
        imp_button.grid(row=0, column=2, padx=5, pady=5)
        delete_button.grid(row=0, column=4, padx=5, pady=5)

        # 添加列表
        self.listbox = tkinter.Listbox(top)
        self.listbox.grid(row=1, column=0, padx=5, pady=5, columnspan=9)

        # 给按钮添加点击事件
        imp_button.bind("<ButtonRelease-1>", self.impMusic)
        play_button.bind("<ButtonRelease-1>", self.playMusic)
        delete_button.bind("<ButtonRelease-1>", self.deleteMusic)

        # 添加音乐列表
        self.flush_list()

        top.mainloop()


if __name__ == "__main__":
    uname = input("请输入用户名:")
    password = input("请输入密码:")
    myservice = MyService()
    if myservice.login(uname, password):
        print('登录成功')
        playWindow = PlayWindow(myservice)
        playWindow.showWindow()

    else:
        print('登录失败')

原网站

版权声明
本文为[出其东门225]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_62218542/article/details/126237333