You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
6.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import os
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
import threading
from doubanSpider.spider import get_subjectID as gID
from doubanSpider.spider import run as run
from doubanSpider.analysis import save_pics
def put_name_to_listb1():
listb1.delete(0, END)
movie = gID()
l3.place(x=80, y=10)
l3_var.set(f'正在搜索...')
l3['background'] = 'yellow'
i = 0
for name in movie:
listb1.insert(i,name)
i += 1
l3.place(x=80, y=10)
l3_var.set(f'搜索完毕!')
l3['background'] = 'green'
def get_content():
# key_word = e1.get()
select_path = c1.get()
print(len(select_path))
# 1.再判断是否选择了磁盘
if len(select_path) == 1:
# 2.判断所选路径是否存在
if not os.path.exists(select_path):
# 3.判断是否在列表框选择了用户名
try:
# 直接获取选中项目
global movie_name_selected
movie_name_selected = listb1.get(listb1.curselection())
select_path = c1.get() + ':/DoubanContent/' + movie_name_selected + '/'
l3.place(x=80, y=10)
l3_var.set(f'正在爬取...')
l3['background'] = 'yellow'
run(movie_name_selected,select_path)
l3.place(x=80, y=10)
l3_var.set(f'爬取完毕!')
l3['background'] = 'green'
# 搜索后但是没选择用户会报TclError错误此except就用来捕获这个异常
except TclError:
messagebox.showwarning(title='警告', message='请选择一个电影!')
l3.place(x=80, y=10)
l3_var.set(f'请选择一个电影!')
l3['background'] = 'red'
# 获取当前选中项目(使用索引)
else:
messagebox.showwarning(title='警告', message='请检查路径!')
l3.place(x=80, y=10)
l3_var.set(f'请检查路径!')
l3['background'] = 'red'
else:
messagebox.showwarning(title='警告', message='您未选择磁盘!')
l3.place(x=80, y=10)
l3_var.set(f'请检查是否选择了磁盘!')
l3['background'] = 'red'
def get_pics():
movie_name_selected = listb1.get(listb1.curselection())
select_path = c1.get() + ':/DoubanContent/' + movie_name_selected + '/'
l3.place(x=80, y=10)
l3_var.set(f'正在分析...')
l3['background'] = 'yellow'
save_pics(movie_name_selected, select_path)
l3.place(x=80, y=10)
l3_var.set(f'分析完毕!')
l3['background'] = 'green'
def open_disk():
disk = c1.get()
big_dir = disk + ':/DoubanContent/'
if len(disk) == 1:
try:
if not os.path.exists(big_dir):
os.mkdir(big_dir)
os.startfile(big_dir)
except:
messagebox.showwarning(title='警告', message='选中的磁盘不存在!')
l3.place(x=80, y=10)
l3_var.set(f'选中的磁盘不存在!')
l3['background'] = 'red'
else:
messagebox.showwarning(title='警告', message='您未选中磁盘!')
l3.place(x=80, y=10)
l3_var.set(f'您未选中磁盘!')
l3['background'] = 'red'
def print_path(event):
# 要使用完整的路径
global disk
disk = c1.get()
disk_path = c1.get() + ':/'
if len(disk) == 1:
if os.path.exists(disk_path):
messagebox.showinfo(title='提示', message=f'文件将存储到:{disk}:/DoubanContent目录下')
else:
messagebox.showerror(title='错误', message='选定磁盘不存在!')
l3.place(x=80, y=10)
l3_var.set(f'选中的磁盘不存在!')
l3['background'] = 'red'
else:
messagebox.showwarning(title='警告', message='请先选定磁盘!')
l3.place(x=80, y=10)
l3_var.set(f'请先选定磁盘!')
l3['background'] = 'red'
def window_quit():
ret = messagebox.askyesno(title='提示', message='是否要退出?')
if ret:
window.destroy()
window.quit()
def escape(event):
window_quit()
def thread_movie(func, *args):
t = threading.Thread(target=func, args=args)
t.setDaemon(True)
t.start()
if __name__ == '__main__':
window = Tk()
width = 600
height = 240
screenWidth = window.winfo_screenwidth() # 获取显示区域的宽度
screenHeight = window.winfo_screenheight() # 获取显示区域的高度
left = (screenWidth - width) / 2
top = (screenHeight - height) / 2
window.geometry("%dx%d+%d+%d" % (width, height, left, top))
window.resizable(0, 0)
window.title('豆瓣影评简单分析软件')
l1 = ttk.Label(window, text='运行状态:')
l1.place(x=13, y=10)
l3_var = StringVar()
l3 = ttk.Label(window, background='yellow', textvar=l3_var)
l3.place(x=80, y=10)
l3_var.set('还没搜索')
l1 = ttk.Label(window, text='本软件用于爬取豆瓣电\n影影评信息并简单分析')
l1.place(x=60, y=60)
l4 = ttk.Label(window, text='磁盘:')
l4.place(x=13, y=120,)
disk_list = ['C', 'D', 'E', 'F', 'G', 'H', 'I']
c1 = ttk.Combobox(window, justify='center', state='readonly', width=17, value=disk_list)
c1.bind('<<ComboboxSelected>>', print_path)
c1.place(x=80, y=120)
b1 = ttk.Button(window, text='搜索', command=lambda: thread_movie(put_name_to_listb1,), width=7)
b1.place(x=100, y=160)
l5 = ttk.Label(window, text='豆瓣新片榜电影列表:')
l5.place(x=250, y=10)
lb1_var = StringVar()
listb1 = Listbox(window, justify='center', listvariable=lb1_var, width=20, height=6)
listb1.place(x=250, y=40, height=140)
b2 = ttk.Button(window, text='开始爬取', command=lambda: thread_movie(get_content, ), width=7)
b2.place(x=290, y=200)
b3 = ttk.Button(window, text='打开文件夹', width=12, command=open_disk)
b3.place(x=450, y=120)
b3 = ttk.Button(window, text='退出', width=7, command=window_quit)
b3.place(x=470, y=200)
b4 = ttk.Button(window, text='分析数据', width=10, command=lambda: thread_movie(get_pics, ))
b4.place(x=455, y=60)
f1 = ttk.LabelFrame(window)
f1.place(x=0, y=190)
l6 = ttk.Label(f1, text='感谢您的使用!', foreground='red')
l6.pack(anchor="w", fill=X)
# 绑定esc键---退出
window.bind('<Escape>', escape)
# 加入主窗口销毁事件
window.protocol('WM_DELETE_WINDOW', window_quit)
window.mainloop()