from tkinter import ttk import tkinter as tk import controller.SpyderController as SC from entity.BilibiliVideo import BilibiliVideo from service.IFileService import IFileService from service.CsvService import CsvService from service.ExcelService import ExcelService class UIController: scRuslt_data=None def main(self): """ UI的主进程,启动UI界面并保持,当return时表示UI界面已经被关闭。 :return: void """ print("zzr1") #一些属性 ##csv文件路径 csv_path=".\csv_file" ## excel_path=".\excel_file" # 创建主窗口 root = tk.Tk() root.title("Python UI") # 设置窗口的初始大小为800x600 root.geometry('800x400') # 定义按钮点击事件处理函数 def on_start_button_click(): # 这里可以添加按钮点击事件的逻辑 print("start_Button clicked!") videoCount=entry1.get() threadCount=entry2.get() waitTime=entry3.get() #创建 SpyderController对象调用其函数 SpyderController=SC.SpyderController() global scRuslt_data scRuslt_data=SpyderController.getBilibiliVideoList(videoCount,threadCount,waitTime) print("爬取完成") def on_stop_button_click(): # 这里可以添加按钮点击事件的逻辑 ##测试数据 # data1 = BilibiliVideo(2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) # data2 = BilibiliVideo(2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) # text_date = [data1, data2] # self.scRuslt_data=text_date if self.scRuslt_data is None: return print("爬取数据实体类为空") for data in self.scRuslt_data: theList = [] theList.append(data.bvId) theList.append(data.title) theList.append(data.url) theList.append(data.topNo) theList.append(data.viewCount) theList.append(data.likeCount) theList.append(data.coinCount) theList.append(data.favoriteCount) theList.append(data.commentCount) theList.append(data.bulletCount) theList.append(data.creatorId) theList.append(data.creatorName) theList.append(data.creatorFanCount) tree.insert("", "end", values=theList) def button_save_to_exce_click(): # 这里可以添加按钮点击事件的逻辑 #print("Button clicked!") EXL = ExcelService() if (self.scRuslt_data != None): EXL.save(excel_path,self.scRuslt_data) else: print("爬取数据实体类为空") def button_save_to_csv_click(): # 这里可以添加按钮点击事件的逻辑 # print("Button clicked!") CS=CsvService() if(self.scRuslt_data!=None): CS.save(csv_path,self.scRuslt_data) else: print("爬取数据实体类为空") # 创建带有标签的输入框 # 创建带有标签的输入框 def add_labeled_entry(parent, label_text, row): label = tk.Label(parent, text=label_text) label.grid(row=row, column=0, sticky=tk.E) # 使用sticky参数使标签右对齐 entry = tk.Entry(parent) entry.grid(row=row, column=1, sticky=tk.W) # 使用sticky参数使输入框左对齐 return entry entry1 = add_labeled_entry(root, "爬取热门视频的总条目数:",0) entry2 = add_labeled_entry(root, "线程数量:",1) entry3 = add_labeled_entry(root, "每次待爬取时间:",2) # 创建一个按钮,点击时调用上面定义的事件处理函数 button_start = tk.Button(root, text="Start", command=on_start_button_click) button_start.grid(row=3, column=1, sticky=tk.W) button_stop = tk.Button(root, text="Stop", command=on_stop_button_click) button_stop.grid(row=4, column=1,sticky=tk.W) button_save_to_excel = tk.Button(root, text="save to excel", command=button_save_to_exce_click) button_save_to_excel.grid(row=5, column=1, sticky=tk.W) button_save_to_csv = tk.Button(root, text="save to excel", command=button_save_to_csv_click) button_save_to_csv.grid(row=6, column=1,sticky=tk.W) # 创建一个带展示框 tree = ttk.Treeview(root, columns=("bvid", "title","url","upload","topNo","viewCount","likeCount","coinCount","favorite","commentCount","bolletCount","creatorld","creatorName","createFanCount")) tree.heading("#1", text="bvid") tree.heading("#2", text="title") tree.heading('#3', text="url") tree.heading('#4', text="upload") tree.heading('#5', text="topNo") tree.heading('#6', text="ViewCount") tree.heading('#7', text="likeCount") tree.heading('#8', text="coinCount") tree.heading('#9', text="favorite") tree.heading('#10', text="commentCount") tree.heading('#11', text="bulletCount") tree.heading('#12', text="creadtorId") tree.heading('#13', text="creatorName") tree.heading('#14', text="createFanCount") # 向展示框添加数据 # 显示表格和滚动条 tree.grid(row=7, column=0,columnspan=10,rowspan=10) # 创建一个水平滚动条 scrollbar_x = tk.Scrollbar(root, orient='horizontal', command=tree.xview) scrollbar_x.grid(row=8, column=0, columnspan=2, sticky='ew') tree.configure(xscrollcommand=scrollbar_x.set) scrollbar_y = tk.Scrollbar(root, orient='vertical', command=tree.yview) scrollbar_y.grid(row=7, column=11, sticky='ns') tree.configure(yscrollcommand=scrollbar_y.set) root.grid_rowconfigure(7, weight=1) root.grid_columnconfigure(1, weight=1) # 运行应用程序 root.mainloop() if __name__ == '__main__': UI = UIController() UI.main()