使用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()