Python用tkinter库制作一个音乐播放器,自定义一个自己想要的播放器。虽然明天其他的播放器强大,如某易云,某狗。但是也是一个好玩的小插件。


1.制作需求


首先要会一点tkinter库的基础使用方法,当然其他gui库也行!这里只是博主这个废物不会其他gui库而用tkinter库来制作。其他的就是Python的一些基础操作而已。

这次播放音乐,博主使用pygame库的模块来制作音乐播放部分。因为这个模块支持很多音乐格式,而且不占用程序运行。用pip安装即可

pip install pygame

1.制作gui部分

我们写制作gui部分,这样再去实现其他功能

主体gui

"""
作者:PYmili
BUG请联系群:706128290
Python版本:3.9.7
"""
import tkinter as tk
import tkinter.messagebox
from tkinter import *
import tkinter.ttk
from PIL import ImageTk
from PIL import Image
import pygame
import time
import os
import re
import requests"""
获取图片详细并处理
return: im
"""
def get_img(filename, width, height):im = Image.open(filename).resize((width, height))im = ImageTk.PhotoImage(im)return im"""
窗口建立
"""
class gui:def __init__(self):#常用参数path,file=os.path.split(__file__)self.path=pathself.file=fileself.video={}self.tk=tk.Tk()def main(self):win=self.tkwin.geometry("1000x563")win.resizable(False,False)win.title("藤和音乐播放器")win.iconbitmap("aum4e-67zg9-001.ico")win.iconbitmap()"""背景图"""canvas_root = tk.Canvas(win, width=1000, height=563)im_root = get_img(fr'{self.path}\img\20170924123431_RE8SX.png', 1000, 563)canvas_root.create_image(500, 277, image=im_root)canvas_root.pack()# VideoListvideolist=Listbox(win,height=11, width=50)videolist.place(x=30,y=40)# Labeltk.Label(win, text="输入文件完整路径:").place(x=1,y=1)# Entryvar_path=tk.StringVar()filepath=tk.Entry(win, width=70, textvariable=var_path).place(x=120,y=1)win.mainloop()

大体gui就是一个有背景的窗口。有一个输入框用于获取用户输入的音乐文件夹。


按钮部分

 # Buttonget_video=Button(win, text="读取文件夹中所有歌曲", command=testing).place(x=80,y=250)play_video=Button(win, text="播放", command=play_video).place(x=35,y=250)delete_=Button(win, text="删除列表", command=delete_).place(x=1, y=300)for_play=Button(win, text="自动播放", command=for_play).place(x=1, y=350)urlvideo=Button(win, text="打开网络音乐播放器", command=url_gui).place(x=1, y=500)win.mainloop()

我们要做几个按钮来供用户操作。


主函数部分

"""
获取用户在输入框输入的文件夹路径
并且处理文件夹下所有音频文件并写入字典
在窗口的列表显示出来
"""# Entryvar_path=tk.StringVar()filepath=tk.Entry(win, width=70, textvariable=var_path).place(x=120,y=1)def testing():if os.path.exists(var_path.get())==True:pathfile=var_path.get()videolists=[]for path,dirs,file in os.walk(pathfile):if any(dirs) == False:for fil in file:videolists.append(path+f"\{fil}")else:for di in dirs:for fil in file:videolists.append(path+di+f"\{fil}")if any(videolists) == False:apap=Falseelse:apap=videolistselse:apap=Nonevideolist.pack()video=[]mps=[]if apap == False:tk.messagebox.showinfo('提示', '你个笨蛋!你不会看你文件夹里面有没有文件吗!')elif apap == None:tk.messagebox.showinfo('提示', '你个笨蛋!你不会看你你的路径是否存在吗!')else:for vi in apap:video.append(vi)yun=Truefor mp in video:file_ext=os.path.splitext(mp)[-1]#print(file_ext)if file_ext in ['.wav', '.mp3', '.ogg', '.flac', 'mp4a']:mps.append(mp)else:passif yun == True:for mpp in mps:p,f=os.path.split(mpp)videolist.insert(END,f)self.video[f]=mppvideolist.place(x=30,y=40)else:tk.messagebox.showwarning("Error", "为识别到可播放文件")

"""
使用pygame播放音乐
"""
def play_video():lines=videolist.curselection()for line in lines:audio=self.video[videolist.get(line)]pygame.mixer.init()pygame.mixer.music.load(audio)pygame.mixer.music.play(2)pygame.mixer.music.get_busy()"""
自动播放音乐部分
"""
def for_play():for key,msg in self.video.items():audio=self.video[key]try:pygame.mixer.init()pygame.mixer.music.load(audio)pygame.mixer.music.play(2)pygame.mixer.music.get_busy()except:continue"""
删除音乐目录
"""
def delete_():for i in range(2000):videolist.delete(i,i)

最终成果及完整代码

源代码

藤和动漫.py

import tkinter as tk
import tkinter.messagebox
from tkinter import *
import tkinter.ttk
from PIL import ImageTk
from PIL import Image
import pygame
import time
import os
import re
import requestsimport get_url_video as url_videodef get_img(filename, width, height):im = Image.open(filename).resize((width, height))im = ImageTk.PhotoImage(im)return imdef url_gui():root=tk.Tk()root.title("UrlVideo-请输入网络音乐完全地址")root.geometry("400x400")root.iconbitmap("aum4e-67zg9-001.ico")Label(root, text="URL地址:").place(x=60,y=1)var=tk.StringVar()url=Entry(root,textvariable=var).pack()def gets():_url_=var.get()try:path=url_video.get_url_video(_url_)Label(root, text="成功").pack()pygame.mixer.init()pygame.mixer.music.load(f"{path}")pygame.mixer.music.play(2)pygame.mixer.music.get_busy()except:Label(root, text="失败").pack()Button(root, text="播放", command=gets).pack()root.mainloop()class gui:def __init__(self):path,file=os.path.split(__file__)self.path=pathself.file=fileself.video={}self.tk=tk.Tk()def main(self):win=self.tkwin.geometry("1000x563")win.resizable(False,False)win.title("藤和音乐播放器")win.iconbitmap("aum4e-67zg9-001.ico")win.iconbitmap()"""背景图"""canvas_root = tk.Canvas(win, width=1000, height=563)im_root = get_img(fr'{self.path}\img\20170924123431_RE8SX.png', 1000, 563)canvas_root.create_image(500, 277, image=im_root)canvas_root.pack()# VideoListvideolist=Listbox(win,height=11, width=50)videolist.place(x=30,y=40)# Labeltk.Label(win, text="输入文件完整路径:").place(x=1,y=1)# Entryvar_path=tk.StringVar()filepath=tk.Entry(win, width=70, textvariable=var_path).place(x=120,y=1)def testing():if os.path.exists(var_path.get())==True:pathfile=var_path.get()videolists=[]for path,dirs,file in os.walk(pathfile):if any(dirs) == False:for fil in file:videolists.append(path+f"\{fil}")else:for di in dirs:for fil in file:videolists.append(path+di+f"\{fil}")if any(videolists) == False:apap=Falseelse:apap=videolistselse:apap=Nonevideolist.pack()video=[]mps=[]if apap == False:tk.messagebox.showinfo('提示', '你个笨蛋!你不会看你文件夹里面有没有文件吗!')elif apap == None:tk.messagebox.showinfo('提示', '你个笨蛋!你不会看你你的路径是否存在吗!')else:for vi in apap:video.append(vi)yun=Truefor mp in video:file_ext=os.path.splitext(mp)[-1]#print(file_ext)if file_ext in ['.wav', '.mp3', '.ogg', '.flac', 'mp4a']:mps.append(mp)else:passif yun == True:for mpp in mps:p,f=os.path.split(mpp)videolist.insert(END,f)self.video[f]=mppvideolist.place(x=30,y=40)else:tk.messagebox.showwarning("Error", "为识别到可播放文件")def play_video():lines=videolist.curselection()for line in lines:audio=self.video[videolist.get(line)]pygame.mixer.init()pygame.mixer.music.load(audio)pygame.mixer.music.play(2)pygame.mixer.music.get_busy()def for_play():for key,msg in self.video.items():audio=self.video[key]try:pygame.mixer.init()pygame.mixer.music.load(audio)pygame.mixer.music.play(2)pygame.mixer.music.get_busy()except:continuedef delete_():for i in range(2000):videolist.delete(i,i)# Buttonget_video=Button(win, text="读取文件夹中所有歌曲", command=testing).place(x=80,y=250)play_video=Button(win, text="播放", command=play_video).place(x=35,y=250)delete_=Button(win, text="删除列表", command=delete_).place(x=1, y=300)for_play=Button(win, text="自动播放", command=for_play).place(x=1, y=350)urlvideo=Button(win, text="打开网络音乐播放器", command=url_gui).place(x=1, y=500)win.mainloop()if __name__ in "__main__":g=gui()g.main()

get_url_video.py文件

import requests
import osdef get_url_video(_url):r=requests.get(url=_url)if r.status_code == 200:path,file=os.path.split(__file__)urls,urlfile=os.path.split(_url)if os.path.exists(fr"{path}\video\{urlfile}") == False:with open(fr"{path}\video\{urlfile}", "wb")as f:f.write(r.content)return fr"{path}\video\{urlfile}"else:return fr"{path}\video\{urlfile}"else:return False

最终成果

 这个就是最终效果,虽然不怎么样.....但是还是实现了我们想要的功能,缺点是没有停止功能,下一首,上一首功能。

这个之后会添加大家可以加QQ群:706128290 关注更新或提交BUG!

群文件中会有文件源文件,而且博主打包了安装,程序可以直接安装到电脑上。

我是PYmili,喜欢就点个赞吧!下次再见!

Python制作音乐播放器相关推荐

  1. 用Python制作音乐播放器(下,含完整源代码)

    哈喽,大家不知道是上午好还是中午好还是下午好还是晚上好! 音乐播放器,大家应该不陌生吧!今天我们一起来学习如何用python制作音乐播放器.之所开头有一个"下",是因为我们以前已经 ...

  2. 用Python制作音乐播放器(上)

    用Python制作简单的音乐播放器 哈喽,大家不知道是上午好还是中午好还是下午好还是晚上好! 音乐播放器,大家应该不陌生吧!今天我们一起来学习如何用python制作音乐播放器.之所开头有一个" ...

  3. python制作音乐播放器_python实现音乐播放器 python实现花框音乐盒子

    本文实例为大家分享了python实现音乐播放器的具体代码,供大家参考,具体内容如下 """这是一个用海龟画图模块和pygame的混音模块制作的简易播放器. 作者:李兴球, ...

  4. Python制作音乐播放器,帮你随时放飞心情~

    最近网易云音乐闹出不少事情,甚至被各大应用商店下架.它的某些做法小笨聪也着实不敢苟同,但还是希望它整改后能够发展更好,当然不只是在故事式热评方面,还包括更为重要的版权问题. 由此,小笨聪也萌发了制作一 ...

  5. 小案例:用Python制作音乐播放器

    以下是音乐播放器要实现的功能: 1.可以通过打开存放音频文件夹来添加音频 2.可以播放和暂停音频 3.可以设置播放音量 需要实现音频播放器的模块: 1.pygame的音频播放功能 2.easygui的 ...

  6. 基于python的音频播放器_基于python实现音乐播放器代码实例

    基于python实现音乐播放器代码实例,一首,函数,按钮,布局,音乐 基于python实现音乐播放器代码实例 易采站长站,站长之家为您整理了基于python实现音乐播放器代码实例的相关内容. 核心播放 ...

  7. 树莓派3B qt+mplayer制作音乐播放器(10)

    内容 树莓派3B qt+mplayer制作音乐播放器:播放.暂停.上一曲.下一曲,音量调节. 平台:树莓派+qt+mplayer 1.配置 qt安装见此: https://blog.csdn.net/ ...

  8. winform制作音乐播放器

    winform制作音乐播放器 本文利用C# 调用Windows自带的Windows Media Player 打造一款属于自己的音乐播放器,以供学习分享使用,如有不足之处,还请指正. 概述 Windo ...

  9. python本地音乐播放器+附源文件地址

    python本地音乐播放器 因为现在听歌都要版权,所以我喜欢把音乐下载下来听.但一直没找到喜欢的本地音乐播放器,我也只会一些python皮毛,所以有了自己写一个python本地音乐播放器的想法,经过摸 ...

  10. 用Python制作简易播放器(电子钢琴) mac系统

    用Python制作简易播放器(电子钢琴) 开发环境:Python3.7 Mac OS 思路: 先根据需要设计GUI的样式,并思考需要定义什么功能 把功能写出来 把功能填入GUI之中 用曲子测试完整的程 ...

最新文章

  1. 李开复看2019投资趋势:最坏的时代将酝酿最伟大的公司
  2. linux下oracle自动创建实例脚本
  3. 光流 | OpenCV实现简单的optical flow(代码类)
  4. 计算机硬盘read,为你解答电脑开机提示a disk read error occurred怎么办
  5. C++总结笔记(八)—— 菱形继承
  6. Servlet异常和错误处理示例教程
  7. QT的QStackedLayout
  8. 如何开始使用Java中的Lambda表达式
  9. pyppeteer-比 selenium 更高效的爬虫利器
  10. 高等数学(第七版)同济大学 习题4-3 个人解答
  11. 数学基础知识总结 —— 7. 行列式的基本知识
  12. datav多组件交互
  13. html5 移动端 开发工具,H5推荐:最好用的五大移动应用开发工具
  14. 前端类库之jQuery
  15. Python程序练习题
  16. 各类参考文献的著录格式及示例
  17. linux css压缩工具下载,JS和CSS的压缩混淆工具(JsCompressor)下载 v3.0
  18. bluetoothd Protocol not available解决方法
  19. Python简单爬虫入门-爬取链家租房网上的租房信息
  20. 如何用python开发一个贪吃蛇游戏_教你一步步利用python实现贪吃蛇游戏

热门文章

  1. 阿里云服务器如何登录?阿里云服务器的三种登录方法...
  2. T19136 交通指挥系统 题解
  3. 遇到安装3dmax2020版本时出现1603错误时解决方法
  4. win10专业版激活方法——亲测可行!!!
  5. php通讯hpsocket,HP-Socket 远程通信 服务端 客户端 源码
  6. Oracle ORA-01033: 错误解决办法
  7. 输入数字怎么变成大写python_用Python将数字转换为中文大写
  8. petalinux install
  9. 卸载极速PDF后鼠标右键还有快捷方式,取消快捷方式的方法
  10. js模板引擎渲染html,JavaScript模板引擎 art-template.js 的使用