diff --git a/controller/UIController.py b/controller/UIController.py index 6503100..6d0f557 100644 --- a/controller/UIController.py +++ b/controller/UIController.py @@ -1,6 +1,157 @@ +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 - """ \ No newline at end of file + """ + + 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() \ No newline at end of file diff --git a/main.py b/main.py index e00c9ae..b67719f 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,11 @@ -from controller.SpyderController import SpyderController +# from controller.SpyderController import SpyderController from controller.UIController import UIController from test.SpyderController_test import TestSpyderController -spyderController = SpyderController() +# spyderController = SpyderController() uiController = UIController() if __name__ == '__main__': - print("this is main.py!") - TestSpyderController().test_main() + # print("this is main.py!") + UI = UIController() + UI.main() \ No newline at end of file diff --git a/service/CsvService.py b/service/CsvService.py index 8df1c85..58b205c 100644 --- a/service/CsvService.py +++ b/service/CsvService.py @@ -1,4 +1,4 @@ -from IFileService import IFileService +from service.IFileService import IFileService from entity.BilibiliVideo import BilibiliVideo diff --git a/service/ExcelService.py b/service/ExcelService.py index f3f488c..1f0b2c6 100644 --- a/service/ExcelService.py +++ b/service/ExcelService.py @@ -1,4 +1,4 @@ -from IFileService import IFileService +from service.IFileService import IFileService from entity.BilibiliVideo import BilibiliVideo diff --git a/test/UIController_test.py b/test/UIController_test.py index 391fe61..362e289 100644 --- a/test/UIController_test.py +++ b/test/UIController_test.py @@ -1,9 +1,49 @@ -from controller.UIController import UIController +import tkinter as tk +from tkinter import ttk -uiController = UIController() +from controller.UIController import UIController class TestUIController: def test_main(self): - # for running test: - uiController.main() + UI = UIController() + UI.main() +# # 创建主窗口 +# root = tk.Tk() +# root.title("带表格的UI界面") +# +# # 使用grid布局定义按钮和输入框 +# button1 = tk.Button(root, text="按钮1") +# button1.grid(row=0, column=0, padx=10, pady=10) +# +# button2 = tk.Button(root, text="按钮2") +# button2.grid(row=0, column=1, padx=10, pady=10) +# +# entry1 = tk.Entry(root) +# entry1.grid(row=1, column=0, padx=10, pady=10) +# +# entry2 = tk.Entry(root) +# entry2.grid(row=1, column=1, padx=10, pady=10) +# +# # 创建表格 +# tree = ttk.Treeview(root, columns=('数据1', '数据2', '数据3'), show='headings') +# tree.heading('数据1', text='列1') +# tree.heading('数据2', text='列2') +# tree.heading('数据3', text='列3') +# tree.grid(row=2, column=0, columnspan=2, sticky='nsew') +# +# # 添加滚动条 +# scrollbar_x = tk.Scrollbar(root, orient='horizontal', command=tree.xview) +# scrollbar_x.grid(row=3, 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=2, column=2, sticky='ns') +# tree.configure(yscrollcommand=scrollbar_y.set) +# +# # 配置grid布局随窗口大小调整 +# root.grid_rowconfigure(2, weight=1) +# root.grid_columnconfigure(1, weight=1) +# +# # 启动Tkinter事件循环 +# root.mainloop()