python develops a simple version of an online music player

  • 2020-05-26 09:33:00
  • OfStack

Online music player, use python Tkinter library to do the one interface, feel the library use rise very convenient, the data of music from netease cloud music one interface, through urllib. urlopen module to open the url, use Json module for data analysis, and finally USES mp3play library to streaming music, can also download mp3 at the same time, the development environment: python2. 7, attach the source code is 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(' Warm prompt ', ' You can search by entering the following \n1. The song name \n2. The singer's name \n3. Part of the lyrics ')
 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=' Search the song ',command=music)
button.pack()
listbox = Listbox(root,width=50)
listbox.bind('<Double-Button-1>',play)
listbox.pack()
mainloop()

Related articles: