python实现音乐播放器 python实现花框音乐盒子


Posted in Python onFebruary 25, 2020

本文实例为大家分享了python实现音乐播放器的具体代码,供大家参考,具体内容如下

"""这是一个用海龟画图模块和pygame的混音模块制作的简易播放器。
作者:李兴球,日期:2018/8/26"""
 
from turtle import *
 
def init_screen():
 """初始化屏幕"""
 screen = Screen()
 screen.setup(width,height)
 screen.bgpic("舞台.png")
 screen.title(gametitle)
 screen.delay(0)
 return screen
 
def init_mixer():
 """初始化混音器,注意在函数内部导入的模块的作用范围"""
 have_pygame = False
 try:
 import pygame
 pygame.mixer.init()
 have_pygame = True
 except:
 pygame = None
 return have_pygame ,pygame
 
class Button(Turtle):
 """按钮类,每个按钮有两张图片,自带音乐"""
 
 def __init__(self,costume_list,x,y,music,width,height):
 Turtle.__init__(self,visible=False)
 self.penup()
 self.costume_list = costume_list # 造型列表
 self.costume_index = 0  # 造型初始索引号
 self.shape(self.costume_list[self.costume_index]) # 设置造型为索引为0的图
 self.goto(x,y)
 self.width = width
 self.height = height
 self.left = x - width/2 # 左边x坐标
 self.right = x + width/2 # 右边x坐标
 self.top = y + height/2 # 上边y坐标
 self.bottom = y - height/2 # 下边y坐标
 self.music = music  
 self.showturtle()
 self.onclick(self.play) # 单击按钮调用play方法
 
 def play(self,x,y):
 """先停止音乐再播放音乐"""
 pygame.mixer.music.stop()  # 停止正在播放的音乐
 pygame.mixer.music.load(self.music)
 screen.title(gametitle + ",正在播放:" + self.music)
 pygame.mixer.music.play(-1,0) # -1表示循环播放,0表示从头开始播放
 
 def onmousemove(self,event):
 """判断鼠标指针是否在按钮坐标范围内"""
 pass
 
def make_button():
 """加载资源,生成播放按钮"""
 c1_list = ("Losing_Sleep0.gif","Losing_Sleep1.gif")
 [screen.addshape(image) for image in c1_list]
 music1 = "Alan Walker - Losing Sleep.mp3"
 b1 = Button(c1_list,-250,0,music1,200,150)
 screen.cv.bind("<Motion>",b1.onmousemove,add=True)
 
 c2_list = ("和兰花在一起0.gif","和兰花在一起1.gif")
 [screen.addshape(image) for image in c2_list]
 music2 = "Yanni - With An Orchid.mp3"
 b2 = Button(c2_list,00,0,music2,200,150)
 screen.cv.bind("<Motion>",b2.onmousemove,add=True)
 
 c3_list = ("Faded0.gif","Faded1.gif")
 [screen.addshape(image) for image in c3_list]
 music3 = "Alan Walker - Faded (纯音乐).wav"
 b3 = Button(c3_list,250,0,music3,200,150)
 screen.cv.bind("<Motion>",b3.onmousemove,add=True)
 
 c4_list = ("兰贵人0.gif","兰贵人1.gif")
 [screen.addshape(image) for image in c4_list]
 music4 = "胡伟立-兰贵人.mp3"
 b4 = Button(c4_list,-250,-200,music4,200,150)
 screen.cv.bind("<Motion>",b4.onmousemove,add=True)
 
 c5_list = ("Spectre0.gif","Spectre1.gif")
 [screen.addshape(image) for image in c5_list]
 music5 = "Alan Walker - Spectre.mp3"
 b5 = Button(c5_list,0,-200,music5,200,150)
 screen.cv.bind("<Motion>",b5.onmousemove,add=True)
 
 c6_list = ("新古典主义0.gif","新古典主义1.gif")
 [screen.addshape(image) for image in c6_list]
 music6 = "新古典主义-组曲.mp3"
 b6 = Button(c6_list,250,-200,music6,200,150)
 screen.cv.bind("<Motion>",b6.onmousemove,add=True)
 
 
if __name__ == "__main__":
 
 gametitle = "花框音乐盒"
 width,height = 800,600
 screen = init_screen()
 mixer_success,pygame = init_mixer()
 if mixer_success:
 print("成功初始化混音器。")
 else:
 print("初始化混音器出现问题。")
 make_button()
 screen.mainloop()

python实现音乐播放器 python实现花框音乐盒子

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python控制台英汉汉英电子词典
Apr 23 Python
简单介绍Python中的几种数据类型
Jan 02 Python
python 按照固定长度分割字符串的方法小结
Apr 30 Python
对python append 与浅拷贝的实例讲解
May 04 Python
python实现图片筛选程序
Oct 24 Python
浅谈pycharm的xmx和xms设置方法
Dec 03 Python
python获取服务器响应cookie的实例
Dec 28 Python
Python标准库使用OrderedDict类的实例讲解
Feb 14 Python
python面试题Python2.x和Python3.x的区别
May 28 Python
Python使用Numpy模块读取文件并绘制图片
May 13 Python
python中sqllite插入numpy数组到数据库的实现方法
Jun 21 Python
python中 Flask Web 表单的使用方法
May 20 Python
python+selenium+PhantomJS抓取网页动态加载内容
Feb 25 #Python
python numpy生成等差数列、等比数列的实例
Feb 25 #Python
信号生成及DFT的python实现方式
Feb 25 #Python
Pycharm远程连接服务器并实现代码同步上传更新功能
Feb 25 #Python
python生成任意频率正弦波方式
Feb 25 #Python
python numpy库linspace相同间隔采样的实现
Feb 25 #Python
Pandas时间序列:时期(period)及其算术运算详解
Feb 25 #Python
You might like
session 的生命周期是多长
2006/10/09 PHP
PHP array_multisort()函数的使用札记
2011/07/03 PHP
php为字符串前后添加指定数量字符的方法
2015/05/04 PHP
24条货真价实的PHP代码优化技巧
2016/07/28 PHP
ThinkPHP发送邮件示例代码
2016/10/08 PHP
php实现留言板功能
2017/03/05 PHP
PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】
2019/12/20 PHP
javascript 建设银行登陆键盘
2008/06/10 Javascript
javascript中怎么做对象的类型判断
2013/11/11 Javascript
javascript格式化json显示实例分析
2015/04/21 Javascript
函数window.open实现关闭所有的子窗口
2015/08/03 Javascript
JavaScript实现使用Canvas绘制图形的基本教程
2016/10/27 Javascript
Javascript实现的StopWatch功能示例
2017/06/13 Javascript
js 两数组去除重复数值的实例
2017/12/06 Javascript
解决小程序无法触发SESSION问题
2020/02/03 Javascript
[03:38]TI4西雅图DOTA2前线报道 71专访
2014/07/08 DOTA
[53:44]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Magma BO3 第一场 1月31日
2021/03/11 DOTA
python中的装饰器详解
2015/04/13 Python
使用Python的Twisted框架编写非阻塞程序的代码示例
2016/05/25 Python
利用Python+Java调用Shell脚本时的死锁陷阱详解
2018/01/24 Python
Matplotlib中文乱码的3种解决方案
2018/11/15 Python
OpenCV+Python识别车牌和字符分割的实现
2019/01/31 Python
在Python 字典中一键对应多个值的实例
2019/02/03 Python
python matplotlib中的subplot函数使用详解
2020/01/19 Python
如何使用Python抓取网页tag操作
2020/02/14 Python
python等差数列求和公式前 100 项的和实例
2020/02/25 Python
浅谈python3打包与拆包在函数的应用详解
2020/05/02 Python
python中封包建立过程实例
2021/02/18 Python
美国药妆网站:EDCskincare.com(防晒、痤疮、抗衰老等)
2017/04/28 全球购物
请介绍一下Ant
2016/07/22 面试题
药店促销活动总结
2014/07/10 职场文书
国际贸易毕业生求职信
2014/07/20 职场文书
2014年国庆节演讲稿
2014/09/02 职场文书
如何写好开幕词?
2019/06/24 职场文书
Python爬虫入门案例之回车桌面壁纸网美女图片采集
2021/10/16 Python
MySQL之select、distinct、limit的使用
2021/11/11 MySQL