Online music Player 1

Source: Internet
Author: User

#-*-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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.