python實(shí)現(xiàn)簡易云音樂播放器
本人最近在學(xué)習(xí)python,在看了一些教程后,用python寫了一個(gè)簡單的云音樂播放器,下面把主要代碼貼上來,其中用到了github上他人寫的一個(gè)漢字轉(zhuǎn)拼音的庫,大家可以在github上找到。
#coding=utf-8 from Tkinter import * import tkMessageBox import urllib import json import mp3play import time import threading from pinyin import PinYin import os import stat test = PinYin() test.load_word() stop=0 def music(): if not entry.get(): tkMessageBox.showinfo("溫馨提示","搜索內(nèi)容不能為空") return name = test.hanzi2pinyin_split(entry.get()) html=urllib.urlopen("http://s.music.163.com/search/get/?type=1&s=%s&limit=9"%name).read() js=json.loads(html) n = 0 global x x = [] for i in js['result']['songs']: listbox.insert(n,'%s(%s)'%(i['name'],i['artists'][0]['name'])) n+=1 x.append(i['audio']) count = 0 #isplaying = None def play(): global count count += 1 index=listbox.curselection() var1.set(u"正在加載"+listbox.get(index,last=None)) urllib.urlretrieve(x[index[0]],'tmp%s.mp3'%str(count)) var1.set(u"正在播放"+listbox.get(index,last=None)) mp3=mp3play.load("tmp%s.mp3"%str(count)) mp3.play() time.sleep(mp3.seconds()) import inspect import ctypes def _async_raise(tid, exctype): """raises the exception, performs cleanup if needed""" tid = ctypes.c_long(tid) if not inspect.isclass(exctype): exctype = type(exctype) res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype)) if res == 0: raise ValueError("invalid thread id") elif res != 1: ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None) raise SystemError("PyThreadState_SetAsyncExc failed") def stop_thread(thread): _async_raise(thread.ident, SystemExit) threads=list() t=None def excute(event): global t for i in threads: stop_thread(i) t = threading.Thread(target=play) t.setDaemon(True) t.start() threads.append(t) root = Tk()#創(chuàng)建一個(gè)窗口 root.title("云音樂") root.geometry("500x300+500+200") entry=Entry(root)#創(chuàng)建輸入框(單行),置父 entry.pack() btn=Button(root,text="搜 索",command=music) btn.pack()#布局方式必須用同一種 var=StringVar() listbox=Listbox(root,width=50,listvariable=var) listbox.bind('<Double-Button-1>',excute) listbox.pack() var1=StringVar() label=Label(root,text="云音樂播放器",fg="purple",textvariable=var1) var1.set("云音樂播放器") label.pack() root.mainloop()#顯示窗口
由于最近事情較多加上我的技術(shù)還不到位,這個(gè)播放器有一個(gè)BUG我還沒有解決,就是在選擇播放第二首歌時(shí),第一首歌不會停止。如果有小伙伴解決了這個(gè)BUG的話,歡迎指正。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python報(bào)錯(cuò)TypeError: ‘dict‘ object is not&
在Python開發(fā)的旅程中,報(bào)錯(cuò)信息就像是一個(gè)個(gè)路障,阻礙著我們前進(jìn)的步伐,而“TypeError: ‘dict’ object is not iterable”這個(gè)報(bào)錯(cuò),常常讓開發(fā)者們陷入困惑,那么,這個(gè)報(bào)錯(cuò)究竟是怎么產(chǎn)生的呢?又該如何有效地解決它呢?讓我們一起深入探討,找到解決問題的方法2024-10-10Python 把序列轉(zhuǎn)換為元組的函數(shù)tuple方法
今天小編就為大家分享一篇Python 把序列轉(zhuǎn)換為元組的函數(shù)tuple方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06詳解Selenium如何使用input標(biāo)簽上傳文件完整流程
這篇文章主要介紹了詳解Selenium如何使用input標(biāo)簽上傳文件完整流程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Python unittest基本使用方法代碼實(shí)例
這篇文章主要介紹了Python unittest基本使用方法代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06