Himusic实战开发

这个作品是先前用来参加比赛(大一,时间有限,技术有限,这个是当时参赛的时候提交.具有一定的参考意义)

效果预览

Himusic实战开发_第1张图片
Himusic实战开发_第2张图片
Himusic实战开发_第3张图片
Himusic实战开发_第4张图片

项目结构

Himusic实战开发_第5张图片
音乐获取模块
这个就是上面的那个 music_get这个光看名字就知道,没什么好说的.这里要说明的时它返回的是一个包含字典的列表.每一个字典包含歌手名字,歌曲,和歌曲的封面,以及解析后的音乐下载地址.这里的话是直接解析了酷我音乐,可以直接播放下载VIP收费音乐.没错我做这个的目的之一就是为了白嫖音乐.
下面是代码:

import requests
import json
from tkinter import messagebox

# 获取音乐数据
def music_text(kw):

    # 请求头
    try:
        headers = {
     
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36 Edg/84.0.522.63",
            "Cookie":"_ga=GA1.2.1083049585.1590317697; _gid=GA1.2.2053211683.1598526974; _gat=1; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1597491567,1598094297,1598096480,1598526974; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1598526974; kw_token=HYZQI4KPK3P",
            "Referer": "http://www.kuwo.cn/search/list?key=%E5%91%A8%E6%9D%B0%E4%BC%A6",
            "csrf": "HYZQI4KPK3P",
        }
        # 参数列表
        params = {
     
            "key": kw,
            # 页数
            "pn": "1",
            # 音乐数
            "rn": "10",
            "httpsStatus": "1",
            "reqId": "cc337fa0-e856-11ea-8e2d-ab61b365fb50",
        }
        # 创建列表,后面下载需要
        music_list = []
        url = "http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?"
        res = requests.get(url = url,headers = headers,params = params,timeout=8)
        res.encoding = "utf-8"
        text = res.text
        # 转成json数据
        json_list = json.loads(text)
        # 发现data中list是存主要数据的地方
        datapack = json_list["data"]["list"]
        #print(datapack)
        # 遍历拿到所需要的数据,音乐名称,歌手,id...
        a = 0
        for i in datapack:
            # 音乐名
            music_pic=i['pic']
            music_name = i["name"]
            # 歌手
            music_singer = i["artist"]
            # 待会需要的id先拿到
            rid = i["rid"]
            # 随便试听拿到一个音乐的接口,这是的rid就用得上了
            api_music = "http://www.kuwo.cn/url?format=mp3&rid={}&response=url&type=convert_url3" \
                        "&br=128kmp3&from=web&t=1598528574799&httpsStatus=1" \
                        "&reqId=72259df1-e85a-11ea-a367-b5a64c5660e5".format(rid)
            api_res = requests.get(url = api_music)
            # 打印发现真实的url确实在里面
            # print(api_res.text)

            music_url = json.loads(api_res.text)["url"]

            # 大功告成,试试效果
            # 把数据存到字典方便下载时查找
            music_dict = {
     }
            music_dict["id"] = a
            music_dict['pic']=music_pic
            music_dict["name"] = music_name
            music_dict["url"] = music_url
            music_dict["singer"] = music_singer
            music_list.append(music_dict)
            a += 1
        return music_list
    except Exception as e:
        messagebox.showinfo('tips','搜索不到请换一首歌呀')
if __name__=='__main__':
    s=''
    list=music_text('十年')
    for i in list:
        print(i)
    print(len(list))

软件界面
其实这个也是我最烦的,好好的一个功能做出来了,结果还要叫一个界面给用户交互,烦死了.
这里的话定义了一个类,main_root

from PIL import Image,ImageTk
import tkinter as tk
import os
import weather
import threading

class  Root:
    '''坑点警告,tkinter的线程容易冲突建议将多个按钮或标签分组打包好一个方法
    否者容易造成线程堵塞冲突,冲而导致功能异常
    此外最好不要使用直接后面place 或者pack等直接防止的方法
    '''



    def __init__(self):

        self.root=tk.Tk()

        self.root.geometry('1100x750')
        self.root.title('Hi音乐v1.01')
        self.root.iconbitmap(r'media\\Hitubiao.ico')
        self.root['background']='white'
        self.root.resizable(width=False,height=False)



    def button_main(self):
        #搜索按钮
        self.sousuo_b=tk.Button(self.root,font=('宋体', 14),relief='solid',text='搜索')
        self.sousuo_b.place(x=750,y=20,width=80,height=30)


        #播放控制按钮
        self.bofang_img=ImageTk.PhotoImage(file='media\\bofang.png')
        self.bofang=tk.Button(self.root,image=self.bofang_img,relief='solid')
        self.bofang.place(x=515,y=610,width=70,height=50)

        self.shangyishou_img=ImageTk.PhotoImage(file='media\\shangyisou.png')
        self.shangyishou = tk.Button(self.root, image=self.shangyishou_img, relief='solid')
        self.shangyishou.place(x=400, y=610, width=100, height=50)

        self.xiayishou_img=ImageTk.PhotoImage(file='media\\xiayishou.png')
        self.xiayishou = tk.Button(self.root, image=self.xiayishou_img, relief='solid')
        self.xiayishou.place(x=595, y=610, width=100, height=50)

        #我的音乐按钮


        self.my_music = tk.Label(self.root, bg='white', fg='gray', font=('宋体', 12), text='我的音乐', justify='left')
        self.my_music.place(x=-10, y=250, width=140, height=20)

        self.my_load_f = tk.Button(self.root,font=('宋体', 10),relief='flat',text='导入本地歌单',bg='white' )
        self.my_load_f.place(x=0,y=280,width=140, height=20)

        self.my_load_love= tk.Button(self.root,font=('宋体', 10),relief='flat',text='我的喜爱歌曲',bg='white' )
        self.my_load_love.place(x=0,y=310,width=140, height=20)

        self.my_load_hand= tk.Button(self.root,font=('宋体', 10),relief='flat',text='已下载的歌曲',bg='white' )
        self.my_load_hand.place(x=0,y=340,width=140, height=20)

        #下载选项按钮

        self.my_load=tk.Button(self.root,font=('宋体', 10),relief='flat',text='下载至我喜爱',bg='white' )
        self.my_load.place(x=0,y=400,width=140, height=20)

        self.my_load_z=tk.Button(self.root,font=('宋体', 10),relief='flat',text='下载至文件中',bg='white' )
        self.my_load_z.place(x=0,y=430,width=140, height=20)

        self.my_load_z_m=tk.Button(self.root,font=('宋体', 10),relief='flat',text='自定义下载至',bg='white' )
        self.my_load_z_m.place(x=0,y=460,width=140, height=20)


    def label_main(self):

        self.image_log=ImageTk.PhotoImage(file='media\\main_look.png')
        self.log=tk.Label(self.root,image=self.image_log,compound="center",text='')
        self.log.place(x=0,y=0,width=140,height=185)
        self.label_log=tk.Label(self.root,text='Hi音乐!',bg='white',fg='red',font=('宋体',12))
        self.label_log.place(x=0,y=185,width=140,height=20)
        self.entry_s=tk.Entry(self.root,font=('宋体', 14),fg='gray',relief='solid')
        self.entry_s.place(x=300,y=20,width=400,height=30)

        #天气获取套件
        self.image_w=ImageTk.PhotoImage(file='media\\weather_black.png')
        self.weather_pic=tk.Label(self.root,bg='white',image=self.image_w).place(x=880,y=20,width=200,height=200)

        self.weather_str=weather.get_weather()
        self.weather_text=tk.Label(self.root,bg='white',text=self.weather_str)
        self.weather_text.place(x=880,y=220,width=200,height=30)

        # 我的音乐标签

        self.my_canv = tk.Canvas(self.root, bg='white').place(x=0, y=225, width=150, height=150)

        #下载选项
        self.my_canv_l = tk.Canvas(self.root, bg='white').place(x=0, y=360, width=150, height=150)

        self.my_music_l = tk.Label(self.root, bg='white', fg='gray', font=('宋体', 12), text='下载选项', justify='left')
        self.my_music_l.place(x=-10, y=370, width=140, height=20)



        #信息展示模块
        self.hi_img=ImageTk.PhotoImage(file='media\\Hi.png')
        self.info_label=tk.Label(self.root,image=self.hi_img,bg='azure',font=('Georgia',15),fg='gray',compound='center',text='')
        self.info_label.place(x=230,y=100,width=600,height=500)

        #self.info_label = tk.Label(self.root, bg='azure').place(x=230, y=100, width=600, height=500)
        # 播放歌曲栏目

        self.my_canv_l = tk.Canvas(self.root, bg='white').place(x=850, y=300, width=200, height=300)

        self.bofang=tk.Label(self.root, bg='white', fg='black', font=('宋体', 12), text='小小展览')
        self.bofang.place(x=880,y=310,width=100,height=20)
        self.see_bofang=tk.Label(self.root,bg='azure',fg='gray', font=('宋体', 12),text='')
        self.see_bofang.place(x=860,y=340,width=170,height=230)

        #进度条使用canvas实现
        self.jindutiao=tk.Canvas(self.root, width=465, height=22, bg="gray")
        self.jindutiao.place(x=230,y=670,width=600,height=10)




        #填充模块不用管
        self.tianchongzuoxia_img=ImageTk.PhotoImage(file='media\\tianchongzuoxia.png')
        self.tianchongzuoxia=tk.Label(self.root,image=self.tianchongzuoxia_img,bg='white').place(x=0,y=510,width=180,height=250)

        self.tianchongyouxia_img=ImageTk.PhotoImage(file='media\\tiancongyouxia.png')
        self.tianchongyouxia=tk.Label(self.root,image=self.tianchongyouxia_img).place(x=850,y=610,width=200,height=130)

    def mainloop(self):
        self.root.mainloop()


if __name__=='__main__':
    root=Root()
    root.label_main()
    root.button_main()
    root.mainloop()

小小播放器
这个就是那个0.5版本的那个播放器
info_mat 这个就是用pygame做出来的,然后说明一下这个eye3这个库,这个可以获取一个MP3文件的时长,这样就可以做出进度条程序了

import tkinter as tk
import pygame

def bofangqi(path):
    global v
    root = tk.Toplevel()
    root.title("音乐播放器")
    root['background']='white'
    root.iconbitmap(r'media\\Hitubiao.ico')
    root.title('Hi音乐播放器v0.5')
    root.resizable(width=False,height=False)
    image_=tk.PhotoImage(file=r'media\bofangqi.png')
    cavans=tk.Canvas(root,bg='white')
    cavans.place(x=0,y=0)
    cavans.create_image(100,50,image = image_)

    v=0.5
    def play():

        pygame.mixer.init()
        pygame.mixer.music.load(path)  # 加载本地文件
        pygame.mixer.music.set_volume(v)
        pygame.mixer.music.play()  # 播放音乐


    def stop():
        pygame.mixer.music.pause() # 停止音乐


    def bofang():

        play()  # 随机播放


    def next_song():  # 下一首
        pygame.mixer.music.unpause()

    def last_song():  # 上一首
        global v
        if v==1:
            return
        v +=0.1
        pygame.mixer.music.set_volume(v)

    def jian():
        global v
        if v == 0:
            return
        v -= 0.1
        pygame.mixer.music.set_volume(v)


    b1 =tk.Button(root, text="+音量", width=10, command=last_song,bg='pink',relief='solid')
    b1.grid(row=0, column=0, padx=10, pady=10)

    b2 = tk.Button(root, text="继续", width=10, command=next_song,bg='pink',relief='solid')
    b2.grid(row=0, column=1, padx=10, pady=10)

    b3 =tk.Button(root, text="-音量", width=10, command=jian,bg='pink',relief='solid')
    b3.grid(row=0, column=2, padx=10, pady=10)

    b4 = tk.Button(root, text="停止", width=20, command=stop,bg='pink',relief='solid')
    b4.grid(row=1, column=0, padx=10, pady=10, columnspan=3)

    b5 = tk.Button(root, text="开始", width=20, command=bofang,bg='pink',relief='solid')
    b5.grid(row=2, column=0, padx=10, pady=10, columnspan=3)
    root.mainloop()
    print('已经执行完毕')

path = 'E:\projects\comper_lungher\project\love music\Red(Rob Simonsen).mp3'
bofangqi(path)

天气获取
这个确实没什么好说的

import urllib.request
import re
import tkinter.messagebox
import tkinter

'''这段代码来自于远古时代懒得去优化了'''
def get_weather():
    try:
        html = urllib.request.urlopen(r'http://www.weather.com.cn/weather/101240201.shtml')
        urllib.request.urlcleanup()
        read = html.read().decode('utf-8')
        def get_way(path,string):
            path_way=path
            path_get=re.compile(path_way)
            ture_key=path_get.findall(string,re.M)
            return str(ture_key)
        path_html='
        see_html=get_way(path_html,read)
        path_see='v.*°C'
        see_weather=get_way(path_see,see_html)
        day=get_way('.*日',see_weather).strip('[\"\']')
        weather=get_way('周.*°C',see_weather).strip('[\']')
        # print(weather)
        return weather
    except Exception as e:
        tkinter.messagebox.showinfo('tips', '请检查您的网络呦')

if __name__=="__main__":
    weather=get_weather()
    print(weather)

主控代码

这个就是Hi_music文件,这里面涉及了很多的信号,由于时间开发比较仓促,所以在当时就是直接用函数在那里堆砌功能,用一堆变量来表示信号,所以就导致后期维护比较麻烦,所以这里比较好的建议是,建立一个信号类,函数执行后在向这个类里面的信号变量更改(发送信号),这样一来就方便管理,也可以省去不少注释.不过下面展示的是一开始写的,不过里面有很多注释还是可以看的.

from main_root import Root
from PIL import ImageTk ,Image
import threading
from music_get import music_text
import  time
import os
import tkinter.messagebox
import tkinter as tk
from urllib.request import urlretrieve
import tkinter.filedialog
import pygame
import eyed3

'''在某一个函数中使用另一个函数的变量要用全局这是毫无疑问的,但是同理要实现某一个功能是需要调用到闭包中的函数变量(内函数调用)也要使用全局
否则会被垃圾机制回收导致功能异常'''

'''
对自定义music_get的说明,这玩意返回一个列表包含字典独享在字典中具有所有的信息

'''

'''这是我们已经开发好的窗口接下来我们只需要对其进行开发调用'''
'''
重点说明
播放选项如下
self.my_load_f :: 导入本地歌单
self.my_load_love :: 我的喜爱歌曲
self.my_load_hand :: 已下载的歌曲
下载选项如下
self.my_load :: 下载至我喜爱
self.my_load_z :: 下载至文件中
self.my_load_z_m :: 自定义下载至
之后是进度条画布
其余的基本使用中文拼音标注
'''



'''设置信号'''
last_sing=''
label_exit=[]

list_sing=[] #用于存放歌曲信息,与搜索功能串通或者本地歌单的导入
#所有的在线相关的歌曲功能都与他关联
id_sing=0
#负责歌曲的切换
'''存放选中的歌曲的信息便于下载与显示'''
music_check={
     }
#线程交互,下载与显示下载中
sing_are_load=0
'''确定是否为网络控制'''
isinternet=1  #1是网络下载的播放 2是已下载 3是我喜欢 4是导入本地
hand_been_down=[]
my_love_music_play=[]
my_think_music_play=[]
page_which=0
show_label_info_hand=[]#负责与已下载(通过网络获取的本地信号)
show_label_info_love=[]
show_label_info_tkink=[]
#显示哪一面
id_hand_down=0
id_my_love=0
id_my_file=0

'''最后重要的信号是播放的信号'''
star_play=1
is_chang_sing=0
is_press_sing=1 #这是主界面播放按钮是否被按压的信号
is_linking = 0
is_bofangqi_do=0

is_press_sing_b = 0#这是在播放器页面stop 与 contuine的信号


'''这是是否下载在线歌曲的东东(信号)'''
hand_been_down_sing=0

'''窗口查看'''
look_bofangqi=[]

#播放的关联函数
def time_get(path):
    try:
        info_m=eyed3.load(path)
        time_info=info_m.info.time_secs
        if time_info>int(time_info):
            time_info=int(time_info)+1
        return time_info
    except :
        pass
def progress(path):

        global  is_press_sing ,is_chang_sing,is_linking ,image_go_progress,image_go,hand_been_down_sing
        global is_press_sing_b,fill_line
        try:

            is_linking = 1
            time_wait=time_get(path)/600

            image_go_progress=ImageTk.PhotoImage(file='media//zanting.png')
            root.bofang.configure(image=image_go_progress)
            fill_line = root.jindutiao.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="cyan")
            x = 600  # 进度条顶点为600
            n = 0 # 初始值为0
            if is_press_sing %2==0:
                is_press_sing_b=2
            while 1:
                if is_chang_sing:
                     break
                if is_press_sing_b==2 :
                    n +=1
                    root.jindutiao.coords(fill_line, (0, 0, n, 60))
                    root.jindutiao.update()
                    # 控制进度条流动的速度
                    time.sleep(time_wait)
                elif is_press_sing_b==1 :
                    time.sleep(1)

                if n==600:
                    break

            fill_line =root.jindutiao.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="gray")

            for t in range(x):
                n +=1
                # 以矩形的长度作为变量值更新
                root.jindutiao.coords(fill_line, (0, 0, n, 60))
                root.root.update()
                time.sleep(0)  # 时间为0,即飞速清空进度条


            image_go=ImageTk.PhotoImage(file='media//bofang.png')
            root.bofang.configure(image=image_go)
            #复位
            is_chang_sing = 0
            is_linking = 0
            is_press_sing = 1
            is_press_sing_b = 0
        except Exception as e:
            pass

def bofangqi(path,name):
    global v ,is_press_sing_b,is_bofangqi_do,isinternet,hand_been_down_sing

    is_bofangqi_do = 1
    if look_bofangqi:
        l=look_bofangqi.pop()
        l.destroy()
    root_ = tk.Toplevel()
    root_.title("音乐播放器")
    root_['background']='white'
    root_.iconbitmap(r'media\\Hitubiao.ico')
    root_.title('Hi音乐播放器v0.5')
    root_.resizable(width=False,height=False)
    image_=tk.PhotoImage(file=r'media\bofangqi.png')
    cavans=tk.Canvas(root_,bg='white')
    cavans.place(x=0,y=0)
    cavans.create_image(100,50,image = image_)
    look_bofangqi.append(root_)
    v=0.5
    def play():

        pygame.mixer.init()
        pygame.mixer.music.load(path)  # 加载本地文件
        pygame.mixer.music.set_volume(v)
        pygame.mixer.music.play()  # 播放音乐


    def stop():
        global is_press_sing_b
        pygame.mixer.music.pause() # 停止音乐
        is_press_sing_b=1 #1是停止



    def bofang():

        play()  #


    def go_again():  # 下一首
        global is_press_sing_b
        pygame.mixer.music.unpause()
        is_press_sing_b = 2


    def add_yinliang():
        global v
        if v==1:
            return
        v +=0.1
        pygame.mixer.music.set_volume(v)

    def jian():
        global v
        if v == 0:
            return
        v -= 0.1
        pygame.mixer.music.set_volume(v)

    play()#一打开就先播放
    if isinternet==3 or isinternet==4:
        str_l=''

        for x in name:
            if x=='(' or x==')':
                x=''
                str_l+='\n'

            if x!='(' or x !=')':
                str_l+=x


        root.see_bofang['text'] = str_l
    if isinternet==2:
        str_l=''

        for x in name:
            if x=='<' or x=='>' :
                x=''
                str_l+='\n'

            if x!='(' or x !=')':
                str_l+=x
        root.see_bofang['text'] = str_l
    if isinternet ==1 :
        pass

    root.see_bofang.configure(font=('宋体',10),fg='red')



    b1 =tk.Button(root_, text="+音量", width=10, command=add_yinliang,bg='pink',relief='solid')
    b1.grid(row=0, column=0, padx=10, pady=10)

    b2 = tk.Button(root_, text="继续", width=10, command=go_again,bg='pink',relief='solid')
    b2.grid(row=0, column=1, padx=10, pady=10)

    b3 =tk.Button(root_, text="-音量", width=10, command=jian,bg='pink',relief='solid')
    b3.grid(row=0, column=2, padx=10, pady=10)

    b4 = tk.Button(root_, text="停止", width=20, command=stop,bg='pink',relief='solid')
    b4.grid(row=1, column=0, padx=10, pady=10, columnspan=3)

    b5 = tk.Button(root_, text="开始", width=20, command=bofang,bg='pink',relief='solid')
    b5.grid(row=2, column=0, padx=10, pady=10, columnspan=3)
    def stop__(xincan):
        global  is_bofangqi_do
        # is_bofangqi_do = 0

        print('00')
        root_.destroy()


    #这tM不写也行,只是由于结构的问题我原来想实现一个功能来着
    root_.protocol("WM_DELETE_WINDOW", lambda : stop__('0'))
    root_.mainloop()


    #复位
    print('00')
    is_bofangqi_do = 0

#负责显示歌曲图片信息
def pic_music_show():

    if list_sing:
        try:
            global  music_img_pic
            path=r'media\view\view.jpg'
            urlretrieve(music_check['pic'],path)
            music_img_pic=Image.open(path)
            music_img_pic=music_img_pic.resize((210,290),Image.ANTIALIAS)
            music_img_pic=ImageTk.PhotoImage(music_img_pic)
            root.see_bofang.configure(image=music_img_pic)
            root.see_bofang.update()

        except:
            pass





'''播放动态图'''

def update_u():
    #global sing_are_load
    unmdex = 25  # 设置帧数
    frames = [tk.PhotoImage(file='media\loading.gif', format='gif -index %i' % (i)) for i in range(unmdex)]

    def update(id_x):
        global sing_are_load
        if sing_are_load:
            sing_are_load = 0

            '''遇事不决直接删除再新建'''
            root.see_bofang.destroy()
            root.see_bofang = tk.Label(root.root, bg='azure',fg='gray', font=('宋体', 12), text='')
            root.see_bofang.place(x=860, y=340, width=170, height=230)
            root.see_bofang['text']='下载完成呦!'


            return
        frame = frames[id_x]
        id_x += 1
        root.see_bofang.configure(image=frame)
        root.root.after(100, update, id_x % unmdex)


    root.root.after(0, update, 0)




#对于在线播放的下载动态图
def update_u_line():
    #global sing_are_load
    unmdex = 25  # 设置帧数
    frames = [tk.PhotoImage(file='media\loading.gif', format='gif -index %i' % (i)) for i in range(unmdex)]

    def update(id_x):
        global sing_are_load
        if sing_are_load:
            sing_are_load=0


            '''遇事不决直接删除再新建'''
            root.see_bofang.destroy()
            root.see_bofang = tk.Label(root.root, bg='azure',fg='gray', font=('宋体', 12), text='')
            root.see_bofang.place(x=860, y=340, width=170, height=230)
            root.see_bofang['text']=list_sing[id_sing]['name']+'\n'+list_sing[id_sing]['singer']

            return
        frame = frames[id_x]
        id_x += 1
        root.see_bofang.configure(image=frame)
        root.root.after(100, update, id_x % unmdex)


    root.root.after(0, update, 0)







'''负责下载歌曲'''
def download(url,path):
    try:
        # data_=requests.get(url)
        # data=data_.content
        # with open(path,'wb') as f:
        #     f.write(data)
        #     f.close()
        urlretrieve(url,path)
        print('111111111111')
    except Exception as e :
        tkinter.messagebox.showinfo('tips', e)
    return





'''主要负责标注选择的歌曲'''
def sing_link(name,y):
    label_sing=tk.Label(root.root,font=('Georgia',15),fg='gold',bg='azure',text=name)
    label_sing.place(x=230,y=y,width=600,height=51)
    label_exit.append(label_sing)
    return

'''搜索的控制函数'''
def sousou_b_com(xincan):
    global last_sing , list_sing ,music_check ,isinternet ,id_sing,is_bofangqi_do,hand_been_down_sing

    hand_been_down_sing=0
    is_bofangqi_do=0
    isinternet=1
    if label_exit:
        b=label_exit.pop()
        b.destroy()
    # if last_sing:
    #     if last_sing==root.entry_s.get():
    #         return
    if list_sing:
        list_sing.clear()
    key=root.entry_s.get()
    last_sing=key
    if  key :
        last_sing=key

        image_sousuozhong=ImageTk.PhotoImage(file='media\\sousuozhong.png')
        root.info_label.configure(image=image_sousuozhong)
        root.info_label['text'] = ''

        root.info_label.update()
        list_sing=music_text(key)
        str_=''
        for i in list_sing:
            str_+=i['name'].strip()+'<-->'+i['singer'].strip()+'\n'+'\n'

        root.info_label.configure(text=str_,font=('Georgia',14),fg='gray')
        root.info_label.update()
        look_link=list_sing[0]['name']+'<-->'+list_sing[0]['singer'].strip()
        sing_link(look_link,100)#初始之位100以45为间隔向下或向上
        music_check['name']=list_sing[0]['name']
        music_check['url'] = list_sing[0]['url']
        music_check['pic'] = list_sing[0]['pic']
        music_check['singer'] = list_sing[0]['singer']
        id_sing=0#复位
        #look_link_=list_sing[id_sing]['name']+'\n'+list_sing[id_sing]['singer'].strip()
        #root.see_bofang['text'] = look_link_
        pic_music_show()

    return


'''下一首歌的函数处理'''
def next_sing(xincan):
    global id_sing ,isinternet,page_which,id_my_love,id_my_file,is_chang_sing,is_bofangqi_do,hand_been_down_sing
    if isinternet==1:
        hand_been_down_sing=0
        #global id_sing
        if list_sing:
            is_chang_sing = 1
            if id_sing+1>= len(list_sing):
                return
            if label_exit:
                b = label_exit.pop()
                b.destroy()
            id_sing +=1
            y=100+45*id_sing

            look_link=list_sing[id_sing]['name']+'<-->'+list_sing[id_sing]['singer'].strip()
            sing_link(look_link,y)#以45为间隔向下或向上
            music_check['name']=list_sing[id_sing]['name']
            music_check['url'] = list_sing[id_sing]['url']
            music_check['pic'] = list_sing[id_sing]['pic']
            music_check['singer'] = list_sing[id_sing]['singer']
            #look_link_=list_sing[id_sing]['name']+'\n'+list_sing[id_sing]['singer'].strip()
            #root.see_bofang['text'] = look_link_
            pic_music_show()
            is_bofangqi_do = 0
    elif isinternet==2:

        global id_hand_down
        if hand_been_down:
            is_chang_sing = 1
            id_hand_down += 1

            if id_hand_down >= len(hand_been_down):
                return
            if label_exit:
                b = label_exit.pop()
                b.destroy()


            if id_hand_down >= (page_which+1)*10 :
                page_which +=1
                root.info_label['text'] = show_label_info_hand[page_which]
                if label_exit:
                    b = label_exit.pop()
                    b.destroy()
            y = 100 + 45 * (id_hand_down - 10 * (page_which))
            print(id_hand_down)
            print(hand_been_down[id_hand_down]['sing_name'])
            sing_link(hand_been_down[id_hand_down]['sing_name'],y)
            is_bofangqi_do = 0



    elif isinternet==3:

        if my_love_music_play:
            is_chang_sing = 1
            id_my_love += 1

            if id_my_love >= len(my_love_music_play):
                return
            if label_exit:
                b = label_exit.pop()
                b.destroy()

            if id_my_love >= (page_which+1)*10 :
                page_which +=1
                root.info_label['text'] = show_label_info_love[page_which]
                if label_exit:
                    b = label_exit.pop()
                    b.destroy()
            y = 100 + 45 * (id_my_love- 10 * (page_which))
            #print(id_my_love)
            #print(my_love_music_play[id_my_love]['sing_name'])
            #print(my_love_music_play)
            sing_link(my_love_music_play[id_my_love]['sing_name'],y)
            is_bofangqi_do = 0


    elif isinternet==4:

        if my_think_music_play:
            is_chang_sing = 1
            id_my_file += 1

            if id_my_file >= len(my_think_music_play):
                return
            if label_exit:
                b = label_exit.pop()
                b.destroy()


            if id_my_file >= (page_which+1)*10 :
                page_which +=1
                root.info_label['text'] = show_label_info_love[page_which]
                if label_exit:
                    b = label_exit.pop()
                    b.destroy()
            y = 100 + 45 * (id_my_file- 10 * (page_which))
            #print(id_my_file)
            #print(my_think_music_play[id_my_file]['sing_name'])
            #print(my_love_music_play)
            sing_link(my_think_music_play[id_my_file]['sing_name'],y)
            is_bofangqi_do = 0








    return


'''上一首歌的处理方式'''

def  last_sing_com(xiuncan):

    global id_sing ,isinternet,page_which,is_chang_sing,is_bofangqi_do,hand_been_down_sing
    if isinternet==1:
        pic_music_show()
        hand_been_down_sing=0
        # global id_sing
        if list_sing:
            is_chang_sing = 1
            if id_sing <=0:
                return
            if label_exit:
                b = label_exit.pop()
                b.destroy()
            id_sing -=1
            y=100+45*id_sing

            look_link=list_sing[id_sing]['name']+'<-->'+list_sing[id_sing]['singer'].strip()
            sing_link(look_link,y)#以45为间隔向下或向上
            music_check['name']=list_sing[id_sing]['name']
            music_check['url'] = list_sing[id_sing]['url']
            music_check['pic'] = list_sing[id_sing]['pic']
            music_check['singer']=list_sing[id_sing]['singer']
            pic_music_show()
            is_bofangqi_do = 0

            #look_link_=list_sing[id_sing]['name']+'\n'+list_sing[id_sing]['singer'].strip()
            #root.see_bofang['text'] = look_link_
            #print(music_check)
            #区别不大最后还得堵塞而且会导致线程堵死页面卡顿所以直接使用单线程
            # t1 = threading.Thread(target=a)
            # t1.setDaemon(True)
            # t1.start()
            # t1.join()
            # t2 = threading.Thread(target=pic_music_show)
            # t2.setDaemon(True)
            # t2.start()
            # t2.join()
    elif isinternet==2:
        global id_hand_down
        if hand_been_down:
            is_chang_sing = 1

            if id_hand_down <= 0:
                return
            if label_exit:
                b = label_exit.pop()
                b.destroy()

            id_hand_down-=1

            if id_hand_down<page_which*10:
                page_which -=1
                root.info_label.destroy()
                root.info_label = tk.Label(root.root, bg='azure', font=('Georgia', 15), fg='gray', compound='center',text='', anchor='n')
                root.info_label.place(x=230, y=100, width=600, height=500)
                root.info_label['text'] = show_label_info_hand[page_which]
                if label_exit:
                    b = label_exit.pop()
                    b.destroy()
            y = 100 + 45 * (id_hand_down - 10 * (page_which))
            #print(id_hand_down)
            #print(hand_been_down[id_hand_down]['sing_name'])
            sing_link(hand_been_down[id_hand_down]['sing_name'],y)
            is_bofangqi_do = 0

    elif isinternet == 3:
        is_chang_sing = 1
        global id_my_love
        if my_love_music_play:

            if id_my_love <= 0:
                return
            if label_exit:
                b = label_exit.pop()
                b.destroy()

            id_my_love -= 1

            if id_my_love < page_which * 10:
                page_which -= 1
                root.info_label.destroy()
                root.info_label = tk.Label(root.root, bg='azure', font=('Georgia', 15), fg='gray', compound='center',text='', anchor='n')
                root.info_label.place(x=230, y=100, width=600, height=500)
                root.info_label['text'] = show_label_info_love[page_which]
                if label_exit:
                    b = label_exit.pop()
                    b.destroy()
            y = 100 + 45 * (id_my_love - 10 * (page_which))
            # print(id_my_love)
            # print(my_love_music_play[id_my_love]['sing_name'])
            sing_link(my_love_music_play[id_my_love]['sing_name'], y)
            is_bofangqi_do = 0

    elif isinternet == 4:
        global id_my_file
        if my_think_music_play:
            is_chang_sing = 1
            if id_my_file <= 0:
                return
            if label_exit:
                b = label_exit.pop()
                b.destroy()

            id_my_file -= 1

            if id_my_file< page_which * 10:
                page_which -= 1
                root.info_label.destroy()
                root.info_label = tk.Label(root.root, bg='azure', font=('Georgia', 15), fg='gray', compound='center',text='', anchor='n')
                root.info_label.place(x=230, y=100, width=600, height=500)
                root.info_label['text'] = show_label_info_tkink[page_which]
                if label_exit:
                    b = label_exit.pop()
                    b.destroy()
            y = 100 + 45 * (id_my_file - 10 * (page_which))
            # print(id_my_love)
            # print(my_love_music_play[id_my_love]['sing_name'])
            sing_link(my_think_music_play[id_my_file]['sing_name'], y)
            is_bofangqi_do = 0

    return#不用管最后函数格式(便于查看缩进结构)


def download_love(xincan):
    global is_bofangqi_do, sing_are_load
    is_bofangqi_do = 0

    if list_sing :
        def a():
            global sing_are_load
            path='love music'+'/'+list_sing[id_sing]['name']+'('+list_sing[id_sing]['singer']+')'+'.mp3'
            download(list_sing[id_sing]['url'],path)
            sing_are_load=1
            dict_={
     }
            dict_['sing_name']=list_sing[id_sing]['name']+'<-->'+list_sing[id_sing]['singer']
            dict_['path']=path
            hand_been_down.append(dict_)

            return
        t1 = threading.Thread(target=a)
        t1.start()
        t2 = threading.Thread(target=update_u)
        t2.start()

    return

def download_myfile(xincan):
     global is_bofangqi_do
     is_bofangqi_do = 0

     if list_sing:
        def a():
            global sing_are_load

            path=tkinter.filedialog.askdirectory()
            name=path+'/'+list_sing[id_sing]['name']+'('+list_sing[id_sing]['singer']+')'+'.mp3'
            download(list_sing[id_sing]['url'], name)
            sing_are_load=1
            hand_been_down.append(music_check)
            # print(hand_been_down)
            dict_={
     }
            dict_['sing_name']=list_sing[id_sing]['name']+'<-->'+list_sing[id_sing]['singer']
            dict_['path']=name
            hand_been_down.append(dict_)
            return

        t1 = threading.Thread(target=a)
        t1.start()
        t2 = threading.Thread(target=update_u)
        t2.start()
     return

def download_my_tk(xincan):
     global is_bofangqi_do
     is_bofangqi_do = 0

     if list_sing:
         def a():
            global sing_are_load
            path=tkinter.filedialog.asksaveasfilename()
            if path:
                name = path+'.mp3'

                download(list_sing[id_sing]['url'],name)
                sing_are_load = 1
                dict_ = {
     }
                dict_['sing_name'] = list_sing[id_sing]['name'] + '<-->' + list_sing[id_sing]['singer']
                dict_['path'] = name
                hand_been_down.append(dict_)
            return

         t1 = threading.Thread(target=a)
         t1.start()
         t2 = threading.Thread(target=update_u)
         t2.start()
     return

#导入已下载
def hand_been_down_music(xincan):
    global isinternet ,page_which ,is_bofangqi_do
    is_bofangqi_do = 0

    # show_label_info_hand=[]
    if hand_been_down:
        page_which = 0  # 复位
        id_sing = 0
        isinternet=2
        i=1
        str_show_=''
        for every in hand_been_down:
            #print(every)
            str_show_i=every['sing_name']+'\n'+'\n'
            str_show_+=str_show_i
            if i % 10 == 0:
                show_label_info_hand.append(str_show_)
                str_show_=''
            elif i==len(hand_been_down):
                show_label_info_hand.append(str_show_)
            i+=1
        root.info_label.destroy()
        root.info_label = tk.Label(root.root, bg='azure', font=('Georgia', 15), fg='gray', compound='center',text='',anchor='n')
        root.info_label.place(x=230, y=100, width=600, height=500)
        root.info_label['text']=show_label_info_hand[page_which]
        sing_link(hand_been_down[id_hand_down]['sing_name'], 100)



#导入我喜欢
def my_like_music(xincan):
    global isinternet ,page_which ,id_my_love ,is_bofangqi_do
    is_bofangqi_do = 0

    path='love music'
    name_list=[]
    m_all = os.listdir(path)
    if m_all:
        # my_love_music_play.clear()
        page_which = 0  # 复位
        id_my_love = 0
        isinternet = 3


        for name_i in m_all:
            #print(name_i)
            if '.mp3' in name_i:
                dict_={
     }


                save_path=path+'/'+name_i
                #print(save_path)

                name_list.append(name_i.replace('.mp3',''))
                dict_['sing_name'] = name_i.replace('.mp3','')
                dict_['path'] = save_path
                #print(dict_)
                my_love_music_play.append(dict_)
        #print(my_love_music_play)
        i=1
        str_show_=''
        for every in name_list:
            #print(every)
            str_show_i=every+'\n'+'\n'
            str_show_+=str_show_i
            if i % 10 == 0:
                show_label_info_love.append(str_show_)
                str_show_=''
            elif i==len(name_list):
                show_label_info_love.append(str_show_)
            i+=1
        root.info_label.destroy()
        root.info_label = tk.Label(root.root, bg='azure', font=('Georgia', 15), fg='gray', compound='center',text='',anchor='n')
        root.info_label.place(x=230, y=100, width=600, height=500)
        root.info_label['text']=show_label_info_love[page_which]
        sing_link(my_love_music_play[id_my_love]['sing_name'],100)
    return

def my_file_music(xincan):

    global isinternet ,page_which ,id_my_file ,is_bofangqi_do
    is_bofangqi_do=0
    path = tkinter.filedialog.askdirectory()
    # print(path)
    name_list=[]
    m_all = os.listdir(path)
    if m_all:
        # my_love_music_play.clear()
        page_which = 0  # 复位
        id_my_file = 0
        isinternet = 4

        for name_i in m_all:
            #print(name_i)
            if '.mp3' in name_i:
                dict_={
     }


                save_path=path+'/'+name_i
                #print(save_path)

                name_list.append(name_i.replace('.mp3',''))
                dict_['sing_name'] = name_i.replace('.mp3','')
                dict_['path'] = save_path
                #print(dict_)
                my_think_music_play.append(dict_)
        print(my_think_music_play)

        i=1
        str_show_=''
        for every in name_list:
            #print(every)
            str_show_i=every+'\n'+'\n'
            str_show_+=str_show_i
            if i % 10 == 0:
                show_label_info_tkink.append(str_show_)
                str_show_=''
            elif i==len(name_list):
                show_label_info_tkink.append(str_show_)
            i+=1
        root.info_label.destroy()
        root.info_label = tk.Label(root.root, bg='azure', font=('Georgia', 15), fg='gray', compound='center',text='',anchor='n')
        root.info_label.place(x=230, y=100, width=600, height=500)
        root.info_label['text']=show_label_info_tkink[page_which]
        sing_link(my_think_music_play[id_my_file]['sing_name'],100)
    return



def player_music(xincan):
    global id_my_file, is_press_sing, is_press_sing_b,id_sing,id_my_love,sing_are_load,id_hand_down,is_linking

    #网络下载的

    if isinternet==1:

        if list_sing:
            is_press_sing += 1
            str___=list_sing[id_sing]['name']+'x'+list_sing[id_sing]['singer'].strip()
            path_music = r'music\{}.mp3'.format(str___)
            # print(path_music)
            name='xincan'
            def a():

                global sing_are_load,hand_been_down_sing
                mucic_url=list_sing[id_sing]['url']
                str___ = list_sing[id_sing]['name']+'x'+ list_sing[id_sing]['singer'].strip()
                path_own = r'music\{}.mp3'.format(str___)
                print(path_own)

                download(mucic_url,path_own)
                sing_are_load = 1
                hand_been_down_sing=1
                print('ok')
                print(hand_been_down_sing)
                print(list_sing[id_sing]['name'])
            if hand_been_down_sing==0:
                t1 = threading.Thread(target=a)
                t1.start()
                t2 = threading.Thread(target=update_u_line)
                t2.start()
            while 1:

                if hand_been_down_sing ==1:
                    break
                time.sleep(1)

            if is_linking == 0:
                def x_1():
                    progress(path_music)

                t1 = threading.Thread(target=x_1)
                t1.start()
            if is_bofangqi_do == 0:
                bofangqi(path_music, name)
            if is_press_sing % 2 == 0:

                is_press_sing_b = 2
                pygame.mixer.music.unpause()

            elif is_press_sing % 2 != 0:
                is_press_sing_b = 1
                pygame.mixer.music.pause()
            return





    #已下载的
    elif isinternet==2:

        path_music=hand_been_down[id_hand_down]['path']
        name=hand_been_down[id_hand_down]['sing_name']
        is_press_sing += 1
        if is_linking == 0:
            def x_1():
                progress(path_music)

            t1 = threading.Thread(target=x_1)
            t1.start()
        if is_bofangqi_do == 0:
            bofangqi(path_music, name)
        if is_press_sing % 2 == 0:

            is_press_sing_b = 2
            pygame.mixer.music.unpause()

        elif is_press_sing % 2 != 0:
            is_press_sing_b = 1
            pygame.mixer.music.pause()

    elif isinternet ==3:

        is_press_sing += 1
        print(is_press_sing)
        if my_love_music_play:
            path_music=my_love_music_play[id_my_love]['path']
            name = my_love_music_play[id_my_love]['sing_name']
            # print(name)
            # print(path_music)

            if is_linking == 0:
                def x_1():
                        progress(path_music)
                t1=threading.Thread(target=x_1)
                t1.start()
            if is_bofangqi_do ==0:
                bofangqi(path_music,name)
            if  is_press_sing % 2 ==0 :

                is_press_sing_b = 2
                pygame.mixer.music.unpause()

            elif  is_press_sing % 2 !=0 :
                is_press_sing_b = 1
                pygame.mixer.music.pause()

    elif isinternet == 4:


        is_press_sing += 1
        if my_think_music_play:
            path_music=my_think_music_play[id_my_file]['path']
            name = my_think_music_play[id_my_file]['sing_name']
            # print(name)
            # print(path_music)

            if is_linking == 0:
                def x_1():
                        progress(path_music)
                t1=threading.Thread(target=x_1)
                t1.start()
            if is_bofangqi_do ==0:
                bofangqi(path_music,name)
            if  is_press_sing % 2 ==0 :

                is_press_sing_b = 2
                pygame.mixer.music.unpause()

            elif  is_press_sing % 2 !=0 :
                is_press_sing_b = 1
                pygame.mixer.music.pause()





root=Root()
root.label_main()#由于布局的问题这两个顺序不要换
root.button_main()

root.sousuo_b.bind('',lambda a:sousou_b_com('0'))
root.xiayishou.bind('',lambda a:next_sing('0'))
root.shangyishou.bind('',lambda a:last_sing_com('0'))

root.my_load.bind('',lambda a:download_love('0'))
root.my_load_z.bind('',lambda a:download_myfile('0'))
root.my_load_z_m.bind('',lambda a:download_my_tk('0'))

root.my_load_hand.bind('',lambda a:hand_been_down_music('0'))
root.my_load_love.bind('',lambda a:my_like_music('0'))
root.my_load_f.bind('',lambda a:my_file_music('0'))


root.bofang.bind('',lambda a:player_music('0'))

root.mainloop()

项目获取

这个将直接在csdn上传项目资源.

你可能感兴趣的:(突发奇想,python,tkinter)