Hello.py 19 KB

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