# _*_ Coding:utf-8 _*_
From Tkinter Import *
Import Tkmessagebox
Import JSON
Import Mp3play
Import re
Import OS
Import Sys
Import Urllib2
Import Pygame
Import time
Import Pymysql
Reload (SYS) #数据库汉子检索
Sys.setdefaultencoding (' UTF8 ')
Conn=pymysql.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' ******* ', db= ' data ', charset= "UTF8") #连接数据库
Cur=conn.cursor () #创建光标
def music ():
Text = Entry.get ()
Text = Text.encode (' Utf-8 ')
If not text:
Tkmessagebox.showinfo (' Warm tip ', ' You can enter the following to search \n1. Song name \n2. Singer's name \n3. Part lyrics ')
Return
Else
sql = ("select * from Music where song_name=%s")
Cur.execute (SQL, text)
line = Cur.fetchall ()
If line== ():
B= "There are no songs in the database you're looking for."
A= "Please re-enter ..."
Listbox.delete (0, listbox.size ())
Listbox.insert (END, B + "..." + a)
Else
For row on line:
NAME=ROW[1]
URL=ROW[2]
Data=urllib2.urlopen (URL). Read ()
Path = "C:\users\administrator\music/songs"
If not Os.path.isdir (path):
Os.mkdir (PATH)
f = Open (path + '/' +name + '. mp3 ', ' WB ')
F.write (data) #写入本地文件
F.close ()
Listbox.delete (0, listbox.size ())
Listbox.insert (end,name + "URL address" +url)
Def play (): #播放功能
Text = Entry.get ()
Text = Text.encode (' Utf-8 ')
sound_file= (' C:\users\administrator\music\songs/%s.mp3 '%text)
Mixer=pygame.mixer
Mixer.init ()
Track = Pygame.mixer.music.load (Sound_file)
Pygame.mixer.music.play (Loops=-1)
def stop (): #停止功能
Pygame.mixer.music.stop ()
root = Tk ()
Root.title ("Music Search")
Root.geometry (' +300+100 ')
Entry = entry (root)
Entry.pack ()
Button = button (root, text= ' search song ', command=music)
Button.pack ()
Button = button (root, text= ' play song ', Command=play)
Button.pack ()
Button = button (root, text= ' pause ', command=stop)
Button.pack ()
ListBox = ListBox (Root, width=100)
Listbox.pack ()
Mainloop ()
A program that is downloaded and played using the MP3 URL in the database