Python uses Tkinter to implement the online music player, pythontkinter
This example uses Tkinter to implement the code of the online music player for your reference. The details are as follows:
1. Use the Tkinter library to write the interface first
2. Write the event triggered by the click button
(1). Use Netease music APIs to encapsulate the returned data into json format and parse the data
(2) The problem related to multithreading has not been solved. By default, a thread is opened for a software program. When a song is played, the display interface is stuck, causing python to stop working.
Code:
# Coding = UTF-8 from Tkinter import * import tkMessageBox import urllib import json import mp3play import time musicList = [] # define the function def music () in the response of a click button (): # print "click" # first determine whether the user has entered the content if E. get () = '': # send a warning. import tkMessageBox first. showinfo ("prompt:", "Enter the content first! ") # Use return. If the preceding conditions are met, do not execute return # Use Netease api to send a request. import urllib # reports an error. encode Chinese Characters in ascii format, can be added to the Link name = E. get (). encode ('utf-8') name = urllib. quote (name) html = urllib. urlopen ('HTTP: // s.music.163.com/search/get /? Type = 1 & s = % s & limit = 9' % name ). read () # return data in json format. You can use regular expression matching to obtain the required data. You can also use json. loads () print html # convert the returned file to json format. to extract the required data, import json a = json. loads (html) # print a [u'result'] [u'songs '] [0] [u'album'] [u'name'] # display all results in the list # print len (a [u'result'] [u'songs '] [0]) for I in range (len (a [u 'result'] [u 'songs '] [0]): # note the insert parameter LB. insert (I, a [u'result'] [u'songs '] [I] [u'album'] [u'name'] + "(" + a [u'result'] [u'songs '] [I] [U'artists '] [0] [u'name'] + ")") # obtain the musicList of the Song url list first. append (a [u'result'] [u'songs '] [I] [u'audio']) # define the double-click LIST Response Function def play (event ): # obtain the returned result curselection () # print LB. curselection () [0] urlnum = LB. curselection () [0] # Streaming Media Playback is not required. Download it and play it again # urllib.urlretrieve(musiclist#urlnum,,'1.mp3') # play a song and call import mp3play # time. sleep (50) clip = mp3play.load('1.mp3 ') clip. play () # Set the playback time import time. sleep (min (300, c Lip. seconds () # failed to respond because of thread issues !!!!!! # One thread can only do one thing at a time. When playing a song, you need to open another thread # create a parent window object, top = Tk () # You can set window attributes, such as title, size top. title ("online music player -- Zhangqiang") top. geometry ('500x300 + 800 + 300 ') # create an editing box and place it on the top of the parent window. Use pack to display E = Entry (top) E. pack () # create Button, define the function triggered by the Button command B = Button (top, text = "Search", command = music) B. pack () # define the LIST Response Function LB = Listbox (top, width = '50', listvariable = StringVar () # bind the trigger event Method-double-click the left button, and Response Function LB. bind ('<Double-Button-1>', play) LB. pack () # Put it to the end # define label labe L = Label (top, text = "Welcome! ", Fg = 'red') label. pack () # send messages cyclically to windows, used to display window top. mainloop ()
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.