Python Simple Music Box

Source: Internet
Author: User

#!/usr/bin/env python
#-*-Coding:utf-8-*-

From Tkinter Import *
Import Tkmessagebox
Import Urllib

def music ():
If e1.get () = = ":
Tkmessagebox.showinfo (' Hint: ', ' Please enter the song name before searching ')
Return
Name = E1.get (). Encode (' Utf-8 ')
Name = Urllib.quote (name)
html = Urllib.urlopen (' http://music.163.com/#/search/m/?id=347230&s=%s&type=1 '%name). Read ()
Print HTML
Def play ():
Pass
top = Tk () #top就等同于窗口
Top.title (' Music Search ') #就是窗口顶部的标题
Top.geometry (' 500x300+600+300 ') #设置窗口的大小: Length 500, Width 300,600 is horizontal, 300 is ordinate
Top.resizable (Width=false,height=false) #设置窗口大小不能变大变小

e1 = Entry (top) #e1 is the search box, Entry (top) is the search box in the top to add
E1.pack () #让搜索框显示
B = Button (top,text= ' search ', command = music). Pack ()
var = stringvar ()
Lb = Listbox (Top,width=50,listvariable=var)
Lb.bind (' <Double-Button-1> ', play)
Lb.pack ()
Top.mainloop ()

################## #可用版 #########################

#coding: Utf-8
From Tkinter Import *
Import Tkmessagebox
Import Urllib
Import JSON
Import Mp3play

List_url = []
List_name = []
def music ():
Text = Entry.get ()
Text = Text.encode (' Utf-8 ')
Text = urllib.quote (text)
if text = = ':
Tkmessagebox.showinfo (' Warm tip ', ' You can enter the following to search \n1. Song name \n2. Singer's name \n3. Part 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 ("Rain Music")
Root.geometry (' +300+100 ')
Entry = entry (root)
Entry.pack ()
button = button (root,text= ' Search song ', command=music)
Button.pack ()
ListBox = ListBox (ROOT,WIDTH=50)
Listbox.bind (' <Double-Button-1> ', play)
Listbox.pack ()
Mainloop ()

Python Simple Music Box

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.