Hello.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. from multiprocessing import Process, Queue, freeze_support
  2. import threading
  3. from _tkinter import TclError
  4. import tkinter
  5. # from tkinter import ttk
  6. import tkinter.font as tkfont
  7. from PIL import ImageTk, Image
  8. import time
  9. import os
  10. import tkinter.messagebox
  11. import webbrowser
  12. import random
  13. import flask
  14. from newtkinter import DragWindow, center_windows
  15. app = flask.Flask(__name__, static_url_path='')
  16. img = None
  17. SCREEN = None
  18. queue_screen = None
  19. draftboard_start = None
  20. datascience_start = None
  21. functionmapping_start = None
  22. functionfactory_start = None
  23. algebraicfactory_start = None
  24. machinelearner_start = None
  25. git_start = None
  26. crawlef_start = None
  27. title_color = '#F0FFFF'
  28. button_color = '#FFFFFF'
  29. button_cursor = 'tcross'
  30. pic_list = os.listdir(f'{os.getcwd()}{os.sep}Pic')
  31. del pic_list[pic_list.index('favicon.ico')]
  32. pic_list = random.sample(pic_list, 10)
  33. class QueueController:
  34. def __init__(self):
  35. self.in_dict = {}
  36. self.out_dict = {}
  37. self.var_dict = {}
  38. self.queue_list = []
  39. self.var_from = {}
  40. self.update_var = lambda x, y: None
  41. self.update_queue = lambda x: None
  42. self.run = False
  43. self.stop_str = "__--$$stop_process$$--__"
  44. def can_stop(self):
  45. return len(self.out_dict) == 0
  46. def __call__(self, *args, **kwargs):
  47. self.run = True
  48. def done():
  49. while self.run:
  50. stop_pid = []
  51. old_var = list(self.var_dict.keys())
  52. for out in self.out_dict:
  53. output: Queue = self.out_dict[out]
  54. if output.empty():
  55. continue
  56. dict_index = f'var_{len(self.var_dict)}'
  57. get_out = output.get()
  58. if get_out == self.stop_str:
  59. stop_pid.append(out)
  60. else:
  61. self.var_dict[dict_index] = get_out
  62. self.var_from[dict_index] = out
  63. if old_var != list(self.var_dict.keys()):
  64. self.update_var(self.var_dict, self.var_from)
  65. if stop_pid:
  66. for i in stop_pid:
  67. del self.in_dict[i]
  68. del self.out_dict[i]
  69. self.queue_list = list(self.in_dict.keys())
  70. self.update_queue(self.queue_list.copy())
  71. t = threading.Thread(target=done)
  72. t.setDaemon(True)
  73. t.start()
  74. return self
  75. def stop(self):
  76. self.run = False
  77. def add_queue(self, inqueue, outqueue, name):
  78. self.stop()
  79. time.sleep(0.5)
  80. self.in_dict[name] = inqueue
  81. self.out_dict[name] = outqueue
  82. self.queue_list = list(self.in_dict.keys())
  83. self.update_queue(self.queue_list.copy())
  84. self.update_var(self.var_dict, self.var_from)
  85. def init(self, update_var, update_queue):
  86. self.update_var = update_var
  87. self.update_queue = update_queue
  88. self.update_queue(list(self.in_dict.keys()))
  89. self.update_var(self.var_dict, self.var_from)
  90. def put(self, value: str, index):
  91. name_space = self.var_dict.copy()
  92. name_space.update(globals())
  93. in_queue = self.in_dict[self.queue_list[index]]
  94. if value.startswith('put_var '):
  95. var_name = value[7:]
  96. in_queue.put(self.var_dict.get(var_name))
  97. elif value.startswith('put_eval '):
  98. in_queue.put(eval(value[8:]), name_space)
  99. elif value.startswith('put_file ') and value.startswith('.py'):
  100. try:
  101. with open(value[4:], 'r') as f:
  102. code_file = f.read()
  103. new_name_space = name_space
  104. exec(code_file, new_name_space)
  105. dict_index = f'var_{len(self.var_dict)}'
  106. in_queue.put(list(new_name_space.keys()))
  107. self.var_dict[dict_index] = new_name_space.copy()
  108. self.var_from[dict_index] = 'self'
  109. except BaseException as e:
  110. in_queue.put(str(e))
  111. else:
  112. in_queue.put(value)
  113. queue_controller = QueueController()
  114. def progress_bar(func):
  115. def run(*agrs, **kwargs):
  116. in_queue, out_queue = func(*agrs, **kwargs)
  117. pid = out_queue.get()
  118. name = func.__name__
  119. queue_controller.add_queue(in_queue, out_queue, f'{name}_{pid}')
  120. queue_controller()
  121. time.sleep(1)
  122. return 'run success'
  123. return run
  124. def draftboard_main(in_queue, out_queue):
  125. out_queue.put(str(os.getpid()))
  126. from draftboard import draw_main
  127. out_queue.put('start')
  128. # 不需要等待
  129. draw_main(in_queue, out_queue)
  130. @app.route('/')
  131. def hello():
  132. return app.send_static_file('Hello.html')
  133. @app.route('/draftboard')
  134. def draftboard():
  135. return draftboard_run()
  136. @progress_bar
  137. def draftboard_run():
  138. in_queue = Queue(10)
  139. out_queue = Queue(10)
  140. Process(target=draftboard_main, args=(in_queue, out_queue)).start()
  141. return in_queue, out_queue
  142. def datascience_main(in_queue, out_queue):
  143. out_queue.put(str(os.getpid()))
  144. from datascience import machine_learning
  145. out_queue.put('start')
  146. time.sleep(0.5)
  147. machine_learning(in_queue, out_queue)
  148. @app.route('/datascience')
  149. def datascience():
  150. return datascience_run()
  151. @progress_bar
  152. def datascience_run():
  153. in_queue = Queue(10)
  154. out_queue = Queue(10)
  155. Process(target=datascience_main, args=(in_queue, out_queue)).start()
  156. return in_queue, out_queue
  157. def functionmapping_main(in_queue, out_queue):
  158. out_queue.put(str(os.getpid()))
  159. from funcsystem.map import function_mapping
  160. out_queue.put('start')
  161. time.sleep(0.5)
  162. function_mapping(in_queue, out_queue)
  163. @app.route('/functionmapping')
  164. def functionmapping():
  165. return functionmapping_run()
  166. @progress_bar
  167. def functionmapping_run():
  168. in_queue = Queue(10)
  169. out_queue = Queue(10)
  170. Process(target=functionmapping_main, args=(in_queue, out_queue)).start()
  171. return in_queue, out_queue
  172. def functionfactory_main(in_queue, out_queue):
  173. out_queue.put(str(os.getpid()))
  174. from funcsystem.factory import function_factory_main
  175. out_queue.put('start')
  176. time.sleep(0.5)
  177. function_factory_main(in_queue, out_queue)
  178. @app.route('/functionfactory')
  179. def functionfactory():
  180. return functionfactory_run()
  181. @progress_bar
  182. def functionfactory_run():
  183. in_queue = Queue(10)
  184. out_queue = Queue(10)
  185. Process(target=functionfactory_main, args=(in_queue, out_queue)).start()
  186. return in_queue, out_queue
  187. def algebraicfactory_main(in_queue, out_queue):
  188. out_queue.put(str(os.getpid()))
  189. from algebraicfactory import algebraic_factory_main
  190. out_queue.put('start')
  191. time.sleep(0.5)
  192. algebraic_factory_main(in_queue, out_queue)
  193. @app.route('/algebraicfactory')
  194. def algebraicfactory():
  195. return algebraicfactory_run()
  196. @progress_bar
  197. def algebraicfactory_run():
  198. in_queue = Queue(10)
  199. out_queue = Queue(10)
  200. Process(target=algebraicfactory_main, args=(in_queue, out_queue)).start()
  201. return in_queue, out_queue
  202. def machinelearner_main(in_queue, out_queue):
  203. out_queue.put(str(os.getpid()))
  204. from machinelearning import machine_learning
  205. out_queue.put('start')
  206. time.sleep(0.5)
  207. machine_learning(in_queue, out_queue)
  208. @app.route('/machinelearner')
  209. def machinelearner():
  210. return machinelearner_run()
  211. @progress_bar
  212. def machinelearner_run():
  213. in_queue = Queue(10)
  214. out_queue = Queue(10)
  215. Process(target=machinelearner_main, args=(in_queue, out_queue)).start()
  216. return in_queue, out_queue
  217. def git_main(in_queue, out_queue):
  218. out_queue.put(str(os.getpid()))
  219. from gitrepo import git_main
  220. out_queue.put('start')
  221. time.sleep(0.5)
  222. git_main(in_queue, out_queue)
  223. @app.route('/git')
  224. def git():
  225. return git_run()
  226. @progress_bar
  227. def git_run():
  228. in_queue = Queue(10)
  229. out_queue = Queue(10)
  230. Process(target=git_main, args=(in_queue, out_queue)).start()
  231. return in_queue, out_queue
  232. def crawler_main(in_queue, out_queue):
  233. out_queue.put(str(os.getpid()))
  234. from crawler import crawler_main
  235. out_queue.put('start')
  236. time.sleep(0.5)
  237. crawler_main(in_queue, out_queue)
  238. @app.route('/crawler')
  239. def crawler():
  240. return crawler_run()
  241. @progress_bar
  242. def crawler_run():
  243. in_queue = Queue(10)
  244. out_queue = Queue(10)
  245. Process(target=crawler_main, args=(in_queue, out_queue)).start()
  246. return in_queue, out_queue
  247. def system_main(in_queue, out_queue):
  248. out_queue.put(str(os.getpid()))
  249. from system.gui import system_main
  250. out_queue.put('start')
  251. time.sleep(0.5)
  252. system_main(in_queue, out_queue)
  253. @app.route('/system')
  254. def system():
  255. return system_run()
  256. @progress_bar
  257. def system_run(): # 不需要进度条
  258. in_queue = Queue(10)
  259. out_queue = Queue(10)
  260. Process(target=system_main, args=(in_queue, out_queue)).start()
  261. return in_queue, out_queue
  262. def queuer():
  263. global title_color, button_color, button_cursor, queue_screen
  264. try:
  265. queue_screen.destroy()
  266. except (AttributeError, TclError):
  267. pass
  268. queue_screen = tkinter.Toplevel()
  269. queue_screen.title('通信管理器')
  270. queue_screen.resizable(width=False, height=False)
  271. queue_screen.geometry(f'+30+30')
  272. font = ("黑体", 11) # 设置字体
  273. def sent():
  274. nonlocal sent_text, queue_box
  275. value = sent_text.get()
  276. try:
  277. index = queue_box.curselection()[0]
  278. except IndexError:
  279. return
  280. queue_controller.put(value, index)
  281. width_b = 20
  282. height_b = 2
  283. a_x = 0
  284. a_y = 0
  285. sent_text = tkinter.Entry(queue_screen, width=width_b * 2)
  286. sent_text.grid(column=a_x, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  287. tkinter.Button(queue_screen, bg=button_color, text='发送', command=sent, font=font, width=10, height=height_b)\
  288. .grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  289. a_y += 1
  290. queue_box = tkinter.Listbox(queue_screen, height=height_b * 8)
  291. queue_box.grid(column=a_x, row=a_y, columnspan=3, rowspan=8, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  292. a_x += 3
  293. a_y = 0
  294. var_box = tkinter.Listbox(queue_screen, width=width_b * 3, height=height_b * 9)
  295. var_box.grid(column=a_x, row=a_y, columnspan=3, rowspan=9, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  296. def update_queue_box(queue_list):
  297. try:
  298. queue_box.delete(0, tkinter.END)
  299. queue_box.insert(0, *queue_list)
  300. except TclError:
  301. pass
  302. def update_var_box(var_dict, var_from):
  303. var = []
  304. for name in var_dict:
  305. var.append(f'{name}[{var_from[name]}] : {var_dict[name]}')
  306. try:
  307. var_box.delete(0, tkinter.END)
  308. var_box.insert(0, *var)
  309. except TclError:
  310. pass
  311. queue_controller.init(update_var_box, update_queue_box)
  312. def to_website():
  313. SCREEN.update()
  314. t = threading.Thread(target=webbrowser.open, args=('https://cotan.songzh.website/',))
  315. t.start()
  316. def close():
  317. global SCREEN
  318. if not queue_controller.can_stop():
  319. tkinter.messagebox.showinfo('操作不被允许', '请先关闭其他模块。')
  320. else:
  321. SCREEN.destroy()
  322. def cotan_main():
  323. global SCREEN, title_color, button_color, button_cursor, img
  324. SCREEN = DragWindow(width=1200, height=800)
  325. font1 = tkfont.Font(family='Comic Sans MS', size=20, weight=tkfont.BOLD)
  326. font2 = tkfont.Font(family='Comic Sans MS', size=16, weight=tkfont.BOLD)
  327. font3 = tkfont.Font(family='Comic Sans MS', size=10)
  328. SCREEN.title('')
  329. SCREEN.resizable(width=False, height=False)
  330. SCREEN.geometry(f'1200x800+30+30')
  331. center_windows(SCREEN, 1200, 800)
  332. # 渲染白色
  333. frame = tkinter.Frame(SCREEN, width=1200, height=800, bg='#FFFFFF')
  334. frame.pack()
  335. # 图片
  336. canvas = tkinter.Canvas(
  337. frame,
  338. bd=0,
  339. width=1000,
  340. height=800,
  341. highlightthickness=0)
  342. pic = pic_list[int(str(time.time()).split()[0][-1])]
  343. bg_image = ImageTk.PhotoImage(Image.open(f'Pic{os.sep}{pic}'))
  344. canvas.create_image(400, 400, image=bg_image)
  345. canvas.grid(column=1, row=0, sticky=tkinter.S + tkinter.N, rowspan=20)
  346. SCREEN.iconbitmap(bitmap=f'Pic{os.sep}favicon.ico', default=f'Pic{os.sep}favicon.ico')
  347. # 标题
  348. tkinter.Label(
  349. frame,
  350. text='CoTan~科学计算系统',
  351. width=20,
  352. bg='#FFFFFF',
  353. font=font1).grid(
  354. column=0,
  355. row=0,
  356. sticky=tkinter.N) # 设置说明
  357. tkinter.Label(
  358. frame,
  359. text='CoTan工具',
  360. bg=title_color,
  361. font=font2).grid(
  362. column=0,
  363. row=1,
  364. sticky=tkinter.W +
  365. tkinter.E)
  366. tkinter.Button(
  367. frame,
  368. text='CoTan草稿板',
  369. cursor=button_cursor,
  370. height=2,
  371. font=font3,
  372. bg=button_color,
  373. command=draftboard_run,
  374. activebackground=title_color,
  375. bd=0,
  376. justify=tkinter.LEFT).grid(
  377. column=0,
  378. row=2,
  379. sticky=tkinter.N +
  380. tkinter.E +
  381. tkinter.W)
  382. tkinter.Button(
  383. frame,
  384. text='自动化网页',
  385. cursor=button_cursor,
  386. command=crawler_run,
  387. height=2,
  388. font=font3,
  389. bg=button_color,
  390. activebackground=title_color,
  391. bd=0,
  392. justify=tkinter.LEFT).grid(
  393. column=0,
  394. row=3,
  395. sticky=tkinter.N +
  396. tkinter.E +
  397. tkinter.W)
  398. tkinter.Button(
  399. frame,
  400. text='Git仓库控制器',
  401. cursor=button_cursor,
  402. command=git_run,
  403. height=1,
  404. font=font3,
  405. bg=button_color,
  406. activebackground=title_color,
  407. bd=0,
  408. justify=tkinter.LEFT).grid(
  409. column=0,
  410. row=4,
  411. sticky=tkinter.N +
  412. tkinter.E +
  413. tkinter.W)
  414. tkinter.Button(
  415. frame,
  416. text='CoTan社区',
  417. cursor=button_cursor,
  418. command=to_website,
  419. height=1,
  420. font=font3,
  421. bg=button_color,
  422. activebackground=title_color,
  423. bd=0,
  424. justify=tkinter.LEFT).grid(
  425. column=0,
  426. row=5,
  427. sticky=tkinter.N +
  428. tkinter.E +
  429. tkinter.W)
  430. title_color = '#FFFAFA'
  431. tkinter.Label(
  432. frame,
  433. text='数学系统',
  434. bg=title_color,
  435. font=font2).grid(
  436. column=0,
  437. row=6,
  438. sticky=tkinter.W +
  439. tkinter.E)
  440. tkinter.Button(
  441. frame,
  442. text='代数工厂',
  443. cursor=button_cursor,
  444. command=algebraicfactory_run,
  445. height=2,
  446. font=font3,
  447. bg=button_color,
  448. activebackground=title_color,
  449. bd=0,
  450. justify=tkinter.LEFT).grid(
  451. column=0,
  452. row=7,
  453. sticky=tkinter.N +
  454. tkinter.E +
  455. tkinter.W)
  456. tkinter.Button(
  457. frame,
  458. text='机器学习',
  459. cursor=button_cursor,
  460. command=machinelearner_run,
  461. height=2,
  462. font=font3,
  463. bg=button_color,
  464. activebackground=title_color,
  465. bd=0,
  466. justify=tkinter.LEFT).grid(
  467. column=0,
  468. row=8,
  469. sticky=tkinter.N +
  470. tkinter.E +
  471. tkinter.W)
  472. tkinter.Button(
  473. frame,
  474. text='数据科学',
  475. cursor=button_cursor,
  476. command=datascience_run,
  477. height=2,
  478. font=font3,
  479. bg=button_color,
  480. activebackground=title_color,
  481. bd=0,
  482. justify=tkinter.LEFT).grid(
  483. column=0,
  484. row=9,
  485. sticky=tkinter.N +
  486. tkinter.E +
  487. tkinter.W)
  488. tkinter.Button(
  489. frame,
  490. text='函数工厂',
  491. cursor=button_cursor,
  492. command=functionfactory_run,
  493. height=1,
  494. font=font3,
  495. bg=button_color,
  496. activebackground=title_color,
  497. bd=0,
  498. justify=tkinter.LEFT).grid(
  499. column=0,
  500. row=10,
  501. sticky=tkinter.N +
  502. tkinter.E +
  503. tkinter.W)
  504. tkinter.Button(
  505. frame,
  506. text='函数实验室',
  507. cursor=button_cursor,
  508. command=functionmapping_run,
  509. height=1,
  510. font=font3,
  511. bg=button_color,
  512. activebackground=title_color,
  513. bd=0,
  514. justify=tkinter.LEFT).grid(
  515. column=0,
  516. row=11,
  517. sticky=tkinter.N +
  518. tkinter.E +
  519. tkinter.W)
  520. title_color = '#F5FFFA'
  521. tkinter.Label(
  522. frame,
  523. text='物化系统',
  524. bg=title_color,
  525. font=font2).grid(
  526. column=0,
  527. row=12,
  528. sticky=tkinter.W +
  529. tkinter.E)
  530. tkinter.Button(
  531. frame,
  532. text='几何车间',
  533. cursor=button_cursor,
  534. height=2,
  535. font=font3,
  536. bg=button_color,
  537. activebackground=title_color,
  538. bd=0,
  539. justify=tkinter.LEFT).grid(
  540. column=0,
  541. row=13,
  542. sticky=tkinter.N +
  543. tkinter.E +
  544. tkinter.W)
  545. tkinter.Button(
  546. frame,
  547. text='物理车间',
  548. cursor=button_cursor,
  549. height=2,
  550. font=font3,
  551. bg=button_color,
  552. activebackground=title_color,
  553. bd=0,
  554. justify=tkinter.LEFT).grid(
  555. column=0,
  556. row=14,
  557. sticky=tkinter.N +
  558. tkinter.E +
  559. tkinter.W)
  560. tkinter.Button(
  561. frame,
  562. text='化学车间',
  563. cursor=button_cursor,
  564. height=1,
  565. font=font3,
  566. bg=button_color,
  567. activebackground=title_color,
  568. bd=0,
  569. justify=tkinter.LEFT).grid(
  570. column=0,
  571. row=15,
  572. sticky=tkinter.N +
  573. tkinter.E +
  574. tkinter.W)
  575. tkinter.Button(
  576. frame,
  577. text='实验室管理',
  578. cursor=button_cursor,
  579. height=1,
  580. font=font3,
  581. bg=button_color,
  582. activebackground=title_color,
  583. bd=0,
  584. justify=tkinter.LEFT).grid(
  585. column=0,
  586. row=16,
  587. sticky=tkinter.N +
  588. tkinter.E +
  589. tkinter.W)
  590. title_color = '#F8F8FF'
  591. tkinter.Label(
  592. frame,
  593. text='其他工具',
  594. bg=title_color,
  595. font=font2).grid(
  596. column=0,
  597. row=17,
  598. sticky=tkinter.W +
  599. tkinter.E)
  600. tkinter.Button(
  601. frame,
  602. text='系统管理',
  603. cursor=button_cursor,
  604. command=system_run,
  605. height=1,
  606. font=font3,
  607. bg=button_color,
  608. activebackground=title_color,
  609. bd=0,
  610. justify=tkinter.LEFT).grid(
  611. column=0,
  612. row=18,
  613. sticky=tkinter.N +
  614. tkinter.E +
  615. tkinter.W)
  616. tkinter.Button(
  617. frame,
  618. text='通信管理器',
  619. cursor=button_cursor,
  620. height=1,
  621. font=font3,
  622. bg=button_color,
  623. command=queuer,
  624. activebackground=title_color,
  625. bd=0,
  626. justify=tkinter.LEFT).grid(
  627. column=0,
  628. row=19,
  629. sticky=tkinter.N +
  630. tkinter.E +
  631. tkinter.W)
  632. tkinter.Label(
  633. frame,
  634. text='',
  635. bg='#FFFFFF',
  636. font=font2,
  637. height=5).grid(
  638. column=0,
  639. row=20,
  640. sticky=tkinter.W +
  641. tkinter.E)
  642. # canvas.create_text(450, 740, text='Welcome to CoTan', font=font4, fill='#FFFFE0')
  643. SCREEN.protocol("WM_DELETE_WINDOW", close)
  644. SCREEN.mainloop()
  645. if __name__ == "__main__":
  646. freeze_support()
  647. app.run()