python实现简易云音乐播放器


Posted in Python onJanuary 04, 2018

本人最近在学习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 相关文章推荐
用smtplib和email封装python发送邮件模块类分享
Feb 17 Python
5种Python单例模式的实现方式
Jan 14 Python
Python实现文件复制删除
Apr 19 Python
tensorflow入门之训练简单的神经网络方法
Feb 26 Python
pandas 层次化索引的实现方法
Jul 06 Python
python实现银行管理系统
Oct 25 Python
python 多进程队列数据处理详解
Dec 23 Python
Django 博客实现简单的全文搜索的示例代码
Feb 17 Python
Python图像处理库PIL中图像格式转换的实现
Feb 26 Python
python+opencv边缘提取与各函数参数解析
Mar 09 Python
django 扩展user用户字段inlines方式
Mar 30 Python
Python 通过爬虫实现GitHub网页的模拟登录的示例代码
Aug 17 Python
Python语言描述连续子数组的最大和
Jan 04 #Python
一个月入门Python爬虫学习,轻松爬取大规模数据
Jan 03 #Python
Python编程pygame模块实现移动的小车示例代码
Jan 03 #Python
python编程实现随机生成多个椭圆实例代码
Jan 03 #Python
Python通过Pygame绘制移动的矩形实例代码
Jan 03 #Python
Python模拟脉冲星伪信号频率实例代码
Jan 03 #Python
Python简单实现socket信息发送与监听功能示例
Jan 03 #Python
You might like
采用PHP函数memory_get_usage获取PHP内存清耗量的方法
2011/12/06 PHP
用PHP实现 上一篇、下一篇的代码
2012/09/29 PHP
php 生成签名及验证签名详解
2016/10/26 PHP
基于Jquery的温度计动画效果
2010/06/18 Javascript
jQuery 中使用JSON的实现代码
2011/12/01 Javascript
JavaScript 判断用户输入的邮箱及手机格式是否正确
2013/12/08 Javascript
查找页面中所有类为test的结点的方法
2014/03/28 Javascript
使用AngularJS创建自定义的过滤器的方法
2015/06/18 Javascript
Angular的事件和表单详解
2016/12/26 Javascript
javascript 面向对象function详解及实例代码
2017/02/28 Javascript
详解vue-cli 3.0 build包太大导致首屏过长的解决方案
2018/11/10 Javascript
PM2自动部署代码步骤流程总结
2018/12/10 Javascript
微信公众号生成新浪短网址的实现(快速生成)
2019/08/18 Javascript
Python 元组(Tuple)操作详解
2014/03/11 Python
python通过加号运算符操作列表的方法
2015/07/28 Python
基于python绘制科赫雪花
2018/06/22 Python
python异步编程 使用yield from过程解析
2019/09/25 Python
python、PyTorch图像读取与numpy转换实例
2020/01/13 Python
pytorch实现mnist数据集的图像可视化及保存
2020/01/14 Python
用python查找统一局域网下ip对应的mac地址
2021/01/13 Python
css3教程之倾斜页面
2014/01/27 HTML / CSS
西班牙在线宠物商店:zooplus.es
2017/02/24 全球购物
购买原创艺术品:Zatista
2019/11/09 全球购物
澳大利亚在线批发商:Simply Wholesale
2021/02/24 全球购物
护理专业推荐信
2013/11/07 职场文书
教师的实习自我鉴定
2013/12/17 职场文书
社区十八大感言
2014/01/19 职场文书
《绿色蝈蝈》教学反思
2014/03/02 职场文书
银行服务明星推荐材料
2014/05/29 职场文书
2014副局长群众路线对照检查材料思想汇报
2014/09/22 职场文书
会计出纳岗位职责
2015/03/31 职场文书
会议新闻稿
2015/07/17 职场文书
PHP连接MSSQL数据库案例,PHPWAMP多个PHP版本连接SQL Server数据库
2021/04/16 PHP
python实战之一步一步教你绘制小猪佩奇
2021/04/22 Python
详解Python为什么不用设计模式
2021/06/24 Python
分享几个实用的CSS代码块
2022/06/10 HTML / CSS