python implements a simple cloud music player

  • 2020-06-23 00:46:16
  • OfStack

I recently learned python, after watching some tutorials, python to write a simple cloud music player, the main code posted below, which used github on others to write a Chinese characters to pinyin library, you can find on 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(" Warm prompt "," The search content cannot be empty ") 
    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" Being loaded "+listbox.get(index,last=None)) 
  urllib.urlretrieve(x[index[0]],'tmp%s.mp3'%str(count)) 
  var1.set(u" There is "+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()# create 1 A window  
root.title(" Cloud music ") 
root.geometry("500x300+500+200") 
entry=Entry(root)# Create input box (single line) , Set the parent  
entry.pack() 
btn=Button(root,text=" search   Mr. ",command=music) 
btn.pack()# The layout must be the same 1 Kind of  
var=StringVar() 
listbox=Listbox(root,width=50,listvariable=var) 
listbox.bind('<Double-Button-1>',excute) 
listbox.pack() 
var1=StringVar() 
label=Label(root,text=" Cloud Music Player ",fg="purple",textvariable=var1) 
var1.set(" Cloud Music Player ") 
label.pack() 
root.mainloop()# Display window 

Due to a lot of things recently and my technology is not in place, I have not solved one BUG of this player, that is, when I choose to play the second song, the first song will not stop. If a friend has solved this BUG, please correct me.


Related articles: