面向对象,使用Tkinter设计界面
import tkinter as tk
import requests, os, winreg, tkinter.messagebox, tkinter.filedialog
class Ui: # 执行前期的ui设计
def __init__(self, window, spider):
self.window = window # 接收实例化的窗口
self.window.title("下载音乐") # 编辑标题
self.window.geometry("400x300") # 设置窗口大小
self.var1 = tk.StringVar() # 定义变量接收选择
self.var = tk.StringVar() # 定义一个tk变量
self.var2 = tk.StringVar() # 定义一个tk变量
def get_desktop():
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders') # 获得桌面路径
return winreg.QueryValueEx(key, "Desktop")[0]
self.path = tk.StringVar()
self.path.set(get_desktop()) # 自定义下载路径
self.spider = spider # 接收实例化的爬虫
self.main() # 自动开启main函数
def main(self):
# 选择下载路径
tk.Label(self.window, text="保存文件路径:").place(x=0, y=10)
tk.Entry(self.window, textvariable=self.path).place(x=90, y=10) # 默认为桌面路径
tk.Button(self.window, text="选择路径", command=self.choice_path, width=10, height=1).place(x=240, y=5)
# 选择下载模式
tk.Label(self.window, text="请选择要使用哪种模式下载:", font=(9,)).place(x=70, y=40)
tk.Radiobutton(self.window, text="歌手:下载该歌手的多首音乐!", variable=self.var1, value="singer", font=(7,)).place(x=50,
y=65)
tk.Radiobutton(self.window, text="歌名:下载要下载的音乐!", variable=self.var1,
value="sing", font=(7,)).place(x=50, y=95) # 定义选择框
# 按钮
tk.Button(