前言
本次案例最终实现效果
开发环境
- python 3.8: 解释器
- pycharm: 代码编辑器
界面代码实现
先导入所需模块
import tkinter as tk
from tkinter import ttk
import tkinter.messagebox
创建窗口
root = tk.Tk()
root.title('XXX') # 名字自己起
root.geometry('900x700+200+200')
root.mainloop()
名称搜索框
可以通过Singers, songs的名字搜索
search_frame = tk.Frame(root)
search_frame.pack(pady=20)
Song_va = tk.StringVar()
tk.Label(search_frame, text='自己起:', font=('黑体', 15)).pack(side=tk.LEFT)
tk.Entry(search_frame, relief='flat', textvariable=Song_va).pack(side=tk.LEFT, padx=10)
tk.Button(search_frame, text='搜索', font=('黑体', 12), relief='flat', bg='#f5b488', padx=10).pack(
side=tk.LEFT, padx=2)
保存框
通过序号保存想要的
Down_va = tk.StringVar()
tk.Label(search_frame, text='songs序号:', font=('黑体', 15)).pack(side=tk.LEFT, anchor='e')
tk.Entry(search_frame, relief='flat', textvariable=Down_va).pack(side=tk.LEFT, padx=10)
tk.Button(search_frame, text='下载', font=('黑体', 12), relief='flat', bg='#f5b488', padx=10).pack(
side=tk.LEFT)
tk.Button(search_frame, text='全部', font=('黑体', 12), relief='flat', bg='#f5b488', padx=10).pack(
side=tk.LEFT, padx=5)
显示界面
columns = ('Num', 'MusicId', 'MusicHash', 'SingerName', 'SongName',)
columns_value = ('序号', 'ID', 'Hash', 'Singers', 'songs')
# 使用ttk创建一个树状图
tree_view = ttk.Treeview(root, height=18, show="headings", columns=columns)
# 设置列名
tree_view.column('Num', width=80, anchor='center')
tree_view.column('MusicId', width=80, anchor='center')
tree_view.column('MusicHash', width=80, anchor='center')
tree_view.column('SingerName', width=80, anchor='center')
tree_view.column('SongName', width=80, anchor='center')
# 给列名设置显示的名字
tree_view.heading('Num', text='序号')
tree_view.heading('MusicId', text='ID')
tree_view.heading('MusicHash', text='Hash')
tree_view.heading('SingerName', text='Singers')
tree_view.heading('SongName', text='songs')
tree_view.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
搜索框功能
def Search():
Song_name = Song_va.get()
if len(Song_name) > 1:
MusicInfo = Music.Music(Song_name)
del_select()
for index, dit in enumerate(MusicInfo):
tree_view.insert('', index + 1, values=(
dit['Num'],
dit['MusicId'],
dit['MusicHash'],
dit['SingerName'],
dit['SongName'],
))
else:
tkinter.messagebox.showerror(title='警告', message='输入有误')
song Download
def Download():
Num = Down_va.get()
Song_name = Song_va.get()
if len(Num) > 1 and len(Song_name) > 1:
MusicInfo = Music.Music(Song_name)
dit = MusicInfo[int(Num)]
Music.GetMusic(dit['MusicHash'], dit['MusicId'])
tkinter.messagebox.showinfo(title='温馨提示', message=f'歌曲{dit["SongName"]}下载完成')
else:
tkinter.messagebox.showerror(title='警告', message='输入有误')
Download 全部 songs
def GetAll():
Song_name = Song_va.get()
if len(Song_name) > 1:
MusicInfo = Music.Music(Song_name)
for dit in MusicInfo:
Music.GetMusic(dit['MusicHash'], dit['MusicId'])
else:
tkinter.messagebox.showerror(title='警告', message='输入有误')
👇问题解答 · 源码获取 · 技术交流 · 抱团学习请联系👇