# _*_ coding:utf-8 _*_from Tkinter import *import tkMessageBoximport jsonimport mp3playimport reimport osimport sysimport urllib2import pygameimport timeimport 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(‘温馨提示‘, ‘您可以输入以下内容进行搜索\n1.歌曲名\n2.歌手名\n3.部分歌词‘) return else: sql = ("select * from music where song_name=%s") cur.execute(sql, text) line = cur.fetchall() if line==(): b= "数据库中没有你要找的歌曲" a="请重新输入..." listbox.delete(0, listbox.size()) listbox.insert(END, b +"......"+ a) else: for row in 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地址"+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("音乐搜索")root.geometry(‘+300+100‘)entry = Entry(root)entry.pack()button = Button(root, text=‘搜索歌曲‘, command=music)button.pack()button = Button(root, text=‘播放歌曲‘, command=play)button.pack()button = Button(root, text=‘暂停‘, command=stop)button.pack()listbox = Listbox(root, width=100)listbox.pack()mainloop()
时间: 2024-10-12 22:57:34