#-*-coding:utf-8-*-
From Tkinter Import *
Import Tkmessagebox #提示
Import Urllib
Import JSON #形成一个字典
Import Mp3play #mp3播放器
Import time
Musiclist =[]
def music ():
Name = Entry.get ()
Name =urllib.quote (Name.encode (' Utf-8 '))
If not name:
Tkmessagebox.showinfo (' Hint: ', ' please enter \ n the singer's name in search! ')
Return #如果输入错误将出现提示
HTML =urllib.urlopen ("Http://s.music.163.com/search/get/?type=1&s=%s&limit=9"%name). Read () #获取网页源代码
Text =json.loads (HTML) #形成字典
List_songs = text[' result ' [' Songs '] #获取源代码
For I in range (len (list_songs)):
Listbox.insert (i,list_songs[i][' name ']+ ' (' +list_songs[i][' artists '][0][' name ']+ ') ')
Musiclist.append (list_songs[i][' audio ') #注意书写格式
Def play (event): #event书写格式不能少
sy = listbox.curselection () [0] Find
Music_url =musiclist[sy]
Urllib.urlretrieve (Music_url, ' 1.mp3 ')
mp3 = mp3play.load (' 1.mp3 ') download
Mp3.play ()
Time.sleep (Mp3.seconds ())
Mp3.stop ()
root = Tk ()
Root.title (' Music Box ')
Root.geometry (' 500x400 ')
Entry = entry (root)
Entry.pack ()
button = button (Text = ' Search ', FG = ' red ', Command=music). Pack () #command连接前边的music函数即搜索的连接
var = stringvar () #添加鼠标双击
ListBox = ListBox (Root,width = ' Listvariable=var ') #鼠标双击
Listbox.bind (' <Double-Button-1> ', play) #鼠标双击
Listbox.pack ()
Label (text= ' welcome ', fg= ' red '). Pack ()
Root.mainloop ()
For I in range (len (list_songs)):
Listbox.insert (i,list_songs[i][' name ']+ ' (' +list_songs[i][' artists '][0][' name ']+ ') ') #取字典的时候要打开所有的分组
Musiclist.append (list_songs[i][' audio ') #与对比
Online music Player 1