python实现简易云音乐播放器

yipeiwu_com5年前Python基础

本人最近在学习python,在看了一些教程后,用python写了一个简单的云音乐播放器,下面把主要代码贴上来,其中用到了github上他人写的一个汉字转拼音的库,大家可以在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("温馨提示","搜索内容不能为空") 
    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()#创建一个窗口 
root.title("云音乐") 
root.geometry("500x300+500+200") 
entry=Entry(root)#创建输入框(单行),置父 
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()#显示窗口

由于最近事情较多加上我的技术还不到位,这个播放器有一个BUG我还没有解决,就是在选择播放第二首歌时,第一首歌不会停止。如果有小伙伴解决了这个BUG的话,欢迎指正。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

详解Django定时任务模块设计与实践

详解Django定时任务模块设计与实践

在开发后台与任务相关的功能中,遇到一个需求:用户需要能够为任务配置定时策略,使任务定时执行某个操作。 需求分析 根据需求,我们可以拆解成如下几个步骤: 「某个操作」的实现 配置...

python数据结构之列表和元组的详解

python数据结构之 列表和元组 序列:序列是一种数据结构,它包含的元素都进行了编号(从0开始)。典型的序列包括列表、字符串和元组。其中,列表是可变的(可以进行修改),而元组和字符串...

django框架事务处理小结【ORM 事务及raw sql,customize sql 事务处理】

本文实例讲述了django框架事务处理。分享给大家供大家参考,具体如下: django 中要求事务处理的情况有两种: 1.基于django orM 的 transaction 处理 2....

Python实现删除当前目录下除当前脚本以外的文件和文件夹实例

本文实例讲述了Python实现删除当前目录下除当前脚本以外的文件和文件夹。分享给大家供大家参考。具体如下: import os,sys import shutil cur_file...

python判断字符串或者集合是否为空的实例

最近在看《Effective Python》,里面提到判断字符串或者集合是否为空的原则,原文如下: Don't check for empty values (like [] or ''...