Home  >  Article  >  Backend Development  >  Python development simple version of online music player sample code

Python development simple version of online music player sample code

高洛峰
高洛峰Original
2017-03-06 11:34:471454browse

Online music player, using python's Tkinter library to create an interface, I feel that this library is quite convenient to use. The music data comes from an interface of NetEase Cloud Music. The URL is opened through the urllib.urlopen module and the Json module is used. Analyze the data, and finally use the mp3play library to play the music online. You can also download mp3 at the same time. Development environment: python2.7. The source code is attached as follows:

# _*_ 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('',play)
listbox.pack()
mainloop()

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.

For more articles related to python development of simple version of online music player sample code, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn