首页 技术 正文
技术 2022年11月22日
0 收藏 579 点赞 4,859 浏览 3162 个字

使用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_linksong_info = {}# 定义适合的函数创建框架和更简洁的按钮, 增加程序的易读性
def labelframe(root):
w = LabelFrame(root)
w.pack(fill=X, padx=15, pady=8)
return wdef 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 wdef frame(root, side):
w = Frame(root)
w.pack(side=side, expand=YES, fill=BOTH, padx=10, pady=8)
return wdef combobox(root, variable, font):
w = ttk.Combobox(root, textvariable=variable, width=10, font=font)
w.pack(fill=X, padx=5, side=LEFT)
return wdef entry(root, font):
w = Entry(root, width=12, font=font)
w.pack(padx=5, fill=X, side=LEFT)
return wdef listbox(root, font):
w = Listbox(root, height=10, width=22, font=font)
w.pack(side=LEFT, fill=X, expand=YES)
return wdef scrollbar(root, orient, side, fill, command=None):
w = Scrollbar(root, orient=orient, command=command)
w.pack(side=side, fill=fill)
return wdef font(family, size):
w = tkFont.Font(family=family, size=size)
return wclass 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()

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,033
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,862