-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.py
443 lines (362 loc) · 16.2 KB
/
MainWindow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import ctypes
import os
import queue
import threading
from datetime import datetime
from tkinter import font
import ttkbootstrap as ttk
from selenium.common import NoSuchWindowException
from ttkbootstrap.style import Bootstyle
from tkinter.filedialog import askdirectory
from ttkbootstrap.dialogs import Messagebox
from ttkbootstrap.constants import *
from tkinter.scrolledtext import ScrolledText
import FuckOnlineCourse
import frozen_dir
from NewMissionWindow import DataEntryForm
from PIL import ImageTk
from SettingWindow import SettingForm
PATH = frozen_dir.app_path()
minTime, maxTime = 3.0, 5.0
class MainFrame(ttk.Frame):
tv = None
mission_list = []
curr_choose_index = 0
mission_begin_btn = None
mission_stop_btn = None
threading_list = []
scroll_text_list = []
output_container = None
after_scroll_text = None
message_list = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.pack(fill=BOTH, expand=YES)
image_files = {
'properties-dark': 'icons8_settings_24px.png',
'properties-light': 'icons8_settings_24px_2.png',
'add-to-backup-dark': 'icons8_add_folder_24px.png',
'add-to-backup-light': 'icons8_add_book_24px.png',
'stop-backup-dark': 'icons8_cancel_24px.png',
'stop-backup-light': 'icons8_cancel_24px_1.png',
'play': 'icons8_play_24px_1.png',
'refresh': 'icons8_refresh_24px_1.png',
'stop-dark': 'icons8_stop_24px.png',
'stop-light': 'icons8_stop_24px_1.png',
'opened-folder': 'icons8_opened_folder_24px.png',
'logo': 'backup.png'
}
self.photoimages = []
imgpath = PATH + os.sep + 'assets' + os.sep
for key, val in image_files.items():
_path = imgpath + val
self.photoimages.append(ImageTk.PhotoImage(name=key, file=_path))
# buttonbar
buttonbar = ttk.Frame(self, style='primary.TFrame')
buttonbar.pack(fill=X, pady=1, side=TOP)
# new backup
_func = lambda: self.create_new_mission()
btn = ttk.Button(
master=buttonbar, text='新建任务',
image='add-to-backup-light',
compound=LEFT,
command=_func
)
btn.pack(side=LEFT, ipadx=5, ipady=5, padx=(1, 0), pady=1)
# backup
_func = lambda: self.mission_begin()
mission_begin_btn = ttk.Button(
master=buttonbar,
text='开始任务',
image='play',
compound=LEFT,
command=_func
)
mission_begin_btn.pack(side=LEFT, ipadx=5, ipady=5, padx=0, pady=1)
self.mission_begin_btn = mission_begin_btn
# # refresh
# _func = lambda: Messagebox.ok(message='我也不知道这个要做啥,哈哈哈哈~~')
# btn = ttk.Button(
# master=buttonbar,
# text='刷新',
# image='refresh',
# compound=LEFT,
# command=_func
# )
# btn.pack(side=LEFT, ipadx=5, ipady=5, padx=0, pady=1)
# stop
_func = lambda: self.stop_mission()
mission_stop_btn = ttk.Button(
master=buttonbar,
text='停止任务',
image='stop-light',
compound=LEFT,
command=_func
)
mission_stop_btn.pack(side=LEFT, ipadx=5, ipady=5, padx=0, pady=1)
mission_stop_btn.config(state=DISABLED)
self.mission_stop_btn = mission_stop_btn
# settings
_func = lambda: self.click_settings()
btn = ttk.Button(
master=buttonbar,
text='设置',
image='properties-light',
compound=LEFT,
command=_func
)
btn.pack(side=LEFT, ipadx=5, ipady=5, padx=0, pady=1)
# left panel
left_panel = ttk.Frame(self, style='bg.TFrame')
left_panel.pack(side=LEFT, fill=Y)
# backup summary (collapsible)
bus_cf = CollapsingFrame(left_panel)
bus_cf.pack(fill=X, pady=1)
# container
bus_frm = ttk.Frame(bus_cf, padding=1)
bus_frm.columnconfigure(1, weight=1)
bus_cf.add(
child=bus_frm,
title='使用教程',
bootstyle=SECONDARY)
custom_font = font.Font(family="微软雅黑", size=10)
notice = ttk.ScrolledText(bus_frm, width=30, font=custom_font, height=20)
notice.insert(ttk.INSERT, '使用教程 ----------\n\n')
notice.insert(ttk.INSERT, '1、使用前必须安装好Chrome浏览器(谷歌浏览器),暂时不支持其他类型浏览器。\n\n')
notice.insert(ttk.INSERT, '2、点击“新建任务”,填好学校、平台和账号等信息,点确定。\n\n')
notice.insert(ttk.INSERT, '3、再在任务栏里选中任务,点击开始任务即可。\n\n')
notice.insert(ttk.INSERT, '4、若需要删除任务,可选中任务后,单击右键再点删除。\n\n')
notice.insert(ttk.INSERT, '注意 ----------\n\n')
notice.insert(ttk.INSERT, '1、新建任务里的平台需要填入网课登录界面的网址!不要填成首页的网址了!\n\n')
notice.insert(ttk.INSERT, '2、使用过程中必须保持屏幕常亮,不能息屏,任务运行过程中打开的浏览器不能最小化,否则会导致操作中断。\n\n')
notice.insert(ttk.INSERT, '3、网课网站有一定概率在凌晨或使用很长一段时间后会将账号强制踢下线,请挑选合适的时间使用。\n\n')
notice.insert(ttk.INSERT, '4、进入到有视频的界面以后,如果视频的章节数与你之前刷的不符合,可以手动调整,但点击频率不要太快\n\n')
notice.insert(ttk.INSERT, '5、本工具仅供学习交流使用,不保证100%不会被检测,请适度使用,如有损失概不负责!\n\n')
notice.insert(ttk.INSERT, '关于 ----------\n\n')
notice.insert(ttk.INSERT, '本工具GitHub项目地址:https://github.com/AsherShelby/FuckingOnlineCourse\n\n')
notice.insert(ttk.INSERT, '欢迎大家支持!^_^\n\n')
notice.configure(state='disabled')
notice.pack()
# logo
lbl = ttk.Label(left_panel, image='logo', style='bg.TLabel')
lbl.pack(side='bottom')
# right panel
right_panel = ttk.Frame(self, padding=(2, 1))
right_panel.pack(side=RIGHT, fill=BOTH, expand=YES)
# Treeview
tv = ttk.Treeview(right_panel, show='headings', height=8)
tv.configure(columns=(
'name', 'state', 'creation-time',
'run-time',
))
tv.column('name', stretch=True)
for col in ['creation-time', 'run-time', 'state']:
tv.column(col, stretch=True)
for col in tv['columns']:
tv.heading(col, text=col.title(), anchor=W)
tv.pack(fill=X, pady=1)
self.tv = tv
def on_click_tree(event):
if len(self.tv.selection()) == 0:
return
item = self.tv.selection()[0]
self.curr_choose_index = self.tv.index(item)
state = self.tv.item(item)
print(state['values'][1])
if state['values'][1] == '运行中':
mission_begin_btn.config(state="disabled")
mission_stop_btn.config(state="normal")
elif state['values'][1] == '待运行':
mission_stop_btn.config(state="disabled")
mission_begin_btn.config(state="normal")
else:
mission_begin_btn.config(state="normal")
mission_stop_btn.config(state="disabled")
# self.after_scroll_text.pack_forget()
# self.scroll_text_list[self.curr_choose_index].pack(fill=BOTH, expand=True)
# self.after_scroll_text = self.scroll_text_list[self.curr_choose_index]
print("You clicked on item:", self.curr_choose_index)
self.tv.bind("<ButtonRelease-1>", on_click_tree)
# scrolling text output
scroll_cf = CollapsingFrame(right_panel)
scroll_cf.pack(fill=BOTH, expand=True)
output_container = ttk.Frame(scroll_cf, padding=1)
self.output_container = output_container
_value = '任务日志'
self.setvar('scroll-message', _value)
st = ScrolledText(output_container)
self.after_scroll_text = st
st.pack(fill=BOTH, expand=True)
scroll_cf.add(output_container, textvariable='scroll-message')
# create right click menu
menu = ttk.Menu(self.tv, tearoff=1)
menu.add_command(label="新建任务", command=self.create_new_mission)
menu.add_separator()
menu.add_command(label="删除", command=self.mission_delete)
def show_menu(event):
menu.post(event.x_root, event.y_root)
self.tv.bind("<Button-3>", show_menu)
def get_directory(self):
""" Open dialogue to get directory and update variable """
self.update_idletasks()
d = askdirectory()
if d:
self.setvar('folder-path', d)
# def update_scroll_text(self, text, index):
# self.scroll_text_list[index].insert(tkinter.INSERT, text)
def mission_begin(self):
if len(self.mission_list) == 0:
Messagebox.ok(message='任务列表为空')
return
self.mission_begin_btn.config(state="disabled")
self.mission_stop_btn.config(state="normal")
selected_item = self.tv.selection()[0]
self.tv.set(selected_item, column='state', value='运行中')
timestamp = datetime.now().strftime('%Y.%d.%m - %H:%M:%S')
self.tv.set(selected_item, column='run-time', value=timestamp)
try:
self.threading_list[self.curr_choose_index].start()
except RuntimeError as er:
self.threading_list.pop(self.curr_choose_index)
t = Thread_with_exception(f'Threading {self.curr_choose_index}', self.mission_list, self.curr_choose_index)
t.daemon = True
self.threading_list.insert(self.curr_choose_index, t)
self.threading_list[self.curr_choose_index].start()
def mission_delete(self):
if len(self.mission_list) == 0:
Messagebox.ok('先新建任务吧哥')
return
selected_item = self.tv.selection()[0]
self.tv.delete(selected_item)
self.mission_list.pop(self.curr_choose_index)
self.threading_list[self.curr_choose_index].killing_self()
self.threading_list.pop(self.curr_choose_index)
self.message_list.pop(self.curr_choose_index)
self.scroll_text_list.pop(self.curr_choose_index)
def create_new_mission(self):
mission_infor_dic = {'name': '', 'school': '', 'id': '', 'password': '', 'platform': ''}
new_mission_window = ttk.Toplevel("新建任务", resizable=(False, False))
new_mission_window.attributes('-topmost', True)
DataEntryForm(new_mission_window, mission_infor_dic)
new_mission_window.geometry(f'600x400+{(new_mission_window.winfo_screenwidth() - 800) // 2}+{(new_mission_window.winfo_screenheight() - 400) // 2}')
new_mission_window.mainloop()
print(mission_infor_dic)
self.mission_list.append(mission_infor_dic)
timestamp = datetime.now().strftime('%Y.%d.%m - %H:%M:%S')
self.tv.insert('', END, values=(mission_infor_dic['name'], '待运行', timestamp, timestamp))
logs = ScrolledText(self.output_container)
self.scroll_text_list.append(logs)
t = Thread_with_exception(f'Threading {self.curr_choose_index}', self.mission_list, self.curr_choose_index)
t.daemon = True
self.threading_list.append(t)
q = queue.Queue()
self.message_list.append(q)
# 已弃用
def stop_mission(self):
if len(self.mission_list) == 0:
Messagebox.ok('任务列表为空')
return
self.mission_stop_btn.config(state="disabled")
selected_item = self.tv.selection()[0]
self.tv.set(selected_item, column='state', value='已停止')
self.mission_begin_btn.config(state='normal')
self.threading_list[self.curr_choose_index].killing_self()
def click_settings(self):
setting_window = ttk.Toplevel("设置", resizable=(False, False))
setting_window.attributes('-topmost', True)
SettingForm(setting_window)
setting_window.geometry(f'600x400+{(setting_window.winfo_screenwidth() - 800) // 2}+{(setting_window.winfo_screenheight() - 400) // 2}')
setting_window.mainloop()
class CollapsingFrame(ttk.Frame):
def __init__(self, master, **kwargs):
super().__init__(master, **kwargs)
self.columnconfigure(0, weight=1)
self.cumulative_rows = 0
# widget images
self.images = [
ttk.PhotoImage(file=PATH + os.path.sep + 'assets' + os.path.sep + 'icons8_double_up_24px.png'),
ttk.PhotoImage(file=PATH + os.path.sep + 'assets' + os.path.sep + 'icons8_double_right_24px.png')
]
def add(self, child, title="", bootstyle=PRIMARY, **kwargs):
"""Add a child to the collapsible frame
Parameters:
child (Frame):
The child frame to add to the widget.
title (str):
The title appearing on the collapsible section header.
bootstyle (str):
The style to apply to the collapsible section header.
**kwargs (Dict):
Other optional keyword arguments.
"""
if child.winfo_class() != 'TFrame':
return
style_color = Bootstyle.ttkstyle_widget_color(bootstyle)
frm = ttk.Frame(self, bootstyle=style_color)
frm.grid(row=self.cumulative_rows, column=0, sticky=EW)
# header title
header = ttk.Label(
master=frm,
text=title,
bootstyle=(style_color, INVERSE)
)
if kwargs.get('textvariable'):
header.configure(textvariable=kwargs.get('textvariable'))
header.pack(side=LEFT, fill=BOTH, padx=10)
# header toggle button
def _func(c=child):
return self._toggle_open_close(c)
btn = ttk.Button(
master=frm,
image=self.images[0],
bootstyle=style_color,
command=_func
)
btn.pack(side=RIGHT)
# assign toggle button to child so that it can be toggled
child.btn = btn
child.grid(row=self.cumulative_rows + 1, column=0, sticky=NSEW)
# increment the row assignment
self.cumulative_rows += 2
def _toggle_open_close(self, child):
"""Open or close the section and change the toggle button
image accordingly.
Parameters:
child (Frame):
The child element to add or remove from grid manager.
"""
if child.winfo_viewable():
child.grid_remove()
child.btn.configure(image=self.images[1])
else:
child.grid()
child.btn.configure(image=self.images[0])
class Thread_with_exception(threading.Thread):
def __init__(self, name, mission_list, curr_choose_index):
threading.Thread.__init__(self)
self.name = name
self.mission_list = mission_list
self.curr_choose_index = curr_choose_index
def run(self):
# target function of the thread class
try: # 用try/finally 的方式处理exception,从而kill thread
print(self.curr_choose_index)
print(self.mission_list)
FuckOnlineCourse.begin(self.mission_list[self.curr_choose_index])
except NoSuchWindowException as e:
print('任务已结束')
def get_id(self):
# returns id of the respective thread
if hasattr(self, '_thread_id'):
return self._thread_id
for id, thread in threading._active.items():
if thread is self:
return id
def killing_self(self):
print('杀死线程')
thread_id = self.get_id()
# 给线程发过去一个exceptions,线程就那边响应完就停了
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, ctypes.py_object(SystemExit))
if res > 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, 0)
print('Exception raise failure')