使用python的内置模块tkinter编写了爬取51Ape网站(无损音乐的百度云链接)的UI界面
tkinter入门简单, 但同时在编写的过程中因为文档的缺少很不方便。
下面是UI界面模块的编写,由于爬虫方面由于网站没有反爬非常简单,就不显示出来了
UI类在初始化时会加载所有歌手信息, 下拉框绑定了<<ComboboxSelected>>事件,请求歌手的歌曲信息显示在listbox, listbox绑定了双击事件请求该歌曲的百度云链接及提取码。
from tkinter import * from tkinter import ttk import tkinter.font as tkFont from .spider import get_singer_info, get_all_song, get_song_link song_info = {} # 定义适合的函数创建框架和更简洁的按钮, 增加程序的易读性 def labelframe(root): w = LabelFrame(root) w.pack(fill=X, padx=15, pady=8) return w def label(root, text, font): w = Label(root, text=text, bg='blue', fg='white', width=6, font=font) w.pack(fill=X, padx=10, side=LEFT) return w def frame(root, side): w = Frame(root) w.pack(side=side, expand=YES, fill=BOTH, padx=10, pady=8) return w def combobox(root, variable, font): w = ttk.Combobox(root, textvariable=variable, width=10, font=font) w.pack(fill=X, padx=5, side=LEFT) return w def entry(root, font): w = Entry(root, width=12, font=font) w.pack(padx=5, fill=X, side=LEFT) return w def listbox(root, font): w = Listbox(root, height=10, width=22, font=font) w.pack(side=LEFT, fill=X, expand=YES) return w def scrollbar(root, orient, side, fill, command=None): w = Scrollbar(root, orient=orient, command=command) w.pack(side=side, fill=fill) return w def font(family, size): w = tkFont.Font(family=family, size=size) return w class AppUI(Frame): def __init__(self): tk = Tk() # 实例化tk对象 self.singer = StringVar() # 定义一个可供内容存取的tkinter的变量 ft = font(family='Calibri', size=10) # 定义字体 family:字体类型名的字符串;size:以点为单位的字体高度 lf = labelframe(tk) # LabelFrame 组件是 Frame 组件的变体。默认情况下,LabelFrame 会在其子组件的周围绘制一个边框以及一个标题。 top_frame = frame(lf, side=TOP) # 定义容器frame,side=TOP sdie停靠在父组件的那一边上,默认TOP label(top_frame, text='Singer', font=ft) # 标签 self.cbb = combobox(top_frame, self.singer, font=ft) # 下拉框 self.cbb['values'] = tuple([key for key in get_singer_info().keys()]) # 初始化下拉框的值 self.cbb.bind("<<ComboboxSelected>>", self.change) # 下拉框绑定选择事件 bottom_frame = frame(lf, TOP) band = frame(bottom_frame, TOP) self.listbox = listbox(band, font=ft) # listbox self.listbox.bind('<Double-Button-1>', self.open_link) # listbox绑定双击事件 vertical_bar = scrollbar(band, orient=VERTICAL, side=RIGHT, fill=Y, command=self.listbox.yview) # 创建滚动条 orient1. 指定绘制 HORIZONTAL(垂直滚动条)还是 VERTICAL(水平滚动条)2. 默认值是 VERTICAL command 1.当滚动条更新时回调的函数 2.通常的是指定对应组件的 xview() 或 yview() 方法 self.listbox['yscrollcommand'] = vertical_bar.set # 设置竖直滚动条 horizontal_bar = scrollbar(bottom_frame, orient=HORIZONTAL, side=BOTTOM, fill=X, command=self.listbox.xview) self.listbox['xscrollcommand'] = horizontal_bar.set # 设置水平滚动条 footer_frame = frame(lf, TOP) label(footer_frame, text='Key', font=ft) self.ekey = entry(footer_frame, font=ft) tk.title('51Api') # 修改窗口名 tk.update() # 刷新页面 刷新winfo_width, winfo_height curWidth = tk.winfo_width() curHeight = tk.winfo_height() scnWidth, scnHeight = tk.maxsize() size = '+%d+%d' % ((scnWidth - curWidth) / 2, (scnHeight - curHeight) / 2) tk.geometry(size) # %dx%d+%d+%d 第一个数横大小,第二个数纵大小,第三个数离左屏幕边界距离,第四个数离上面屏幕边界距离 tk.mainloop() # 进入消息循环 def change(self, event): self.listbox.delete(0, END) get_all_song(self.cbb.get()) from .spider import song_queue while True: if song_queue.empty(): break song = song_queue.get() self.listbox.insert('end', song[0]) song_info[song[0]] = song[1] def open_link(self, event): from webbrowser import open down_link, down_key = get_song_link(song_info[self.listbox.selection_get()]) self.ekey.delete(0, END) self.ekey.insert(0, down_key) open(down_link) if __name__ == "__main__": AppUI()