python开发简易版在线音乐播放器

yipeiwu_com6年前Python基础

在线音乐播放器,使用python的Tkinter库做了一个界面,感觉这个库使用起来还是挺方便的,音乐的数据来自网易云音乐的一个接口,通过urllib.urlopen模块打开网址,使用Json模块进行数据的解析,最后使用mp3play库对音乐进行在线播放,也可以同时下载mp3,开发环境:python2.7,附上源代码如下:

# _*_ coding:utf-8 _*_
from Tkinter import *
import tkMessageBox
import urllib
import json
import mp3play
 
def music():
 text = entry.get()
 text = text.encode('utf-8')
 text = urllib.quote(text)
 if not text:
 tkMessageBox.showinfo('温馨提示', '您可以输入以下内容进行搜索\n1.歌曲名\n2.歌手名\n3.部分歌词')
 return
 html=urllib.urlopen('http://s.music.163.com/search/get/?type=1&s=%s&limit=9' %text).read()
 text = json.loads(html)
 list_s = text['result']['songs']
 list_url = []
 global list_url
 list_name = []
 global list_name
 listbox.delete(0,listbox.size())
 for i in list_s:
 listbox.insert(END,i['name']+ "("+i['artists'][0]['name']+")")
 list_url.append(i['audio'])
 list_name.append(i['name'])
 
def play(event):
 global mp3
 sy = listbox.curselection()[0]
 mp3 = mp3play.load(list_url[sy])
 mp3.play()
 urllib.urlretrieve(list_url[sy], list_name[sy] + '.mp3')
 
root = Tk()
root.title("Tkinter Music")
root.geometry('+300+100')
entry = Entry(root)
entry.pack()
button = Button(root,text='搜索歌曲',command=music)
button.pack()
listbox = Listbox(root,width=50)
listbox.bind('<Double-Button-1>',play)
listbox.pack()
mainloop()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

剖析Python的Tornado框架中session支持的实现代码

tornado 里面没有 session?不,当然有~我知道 github 上肯定有人帮我写好了~ O(∩_∩)O~       于是乎...

Python利用pandas计算多个CSV文件数据值的实例

功能:扫描当前目录下所有CSV文件并对其中文件进行统计,输出统计值到CSV文件 pip install pandas import pandas as pd import glob...

Python 网络编程说明第1/2页

一、网络知识的一些介绍 socket 是网络连接端点。例如当你的Web浏览器请求www.jb51.net上的主页时,你的Web浏览器创建一个socket并命令它去连接 www.jb51....

使用Pandas将inf, nan转化成特定的值

使用Pandas将inf, nan转化成特定的值

1. 数据处理中很恶心,出现 RuntimeWarning: divide by zero encountered in divide 发现自己的DataFrame中有除以0的运算,出...

Python多线程编程(二):启动线程的两种方法

在Python中我们主要是通过thread和threading这两个模块来实现的,其中Python的threading模块是对thread做了一些包装的,可以更加方便的被使用,所以我们使...