toolbox.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. from tkinter.colorchooser import askcolor
  2. from newtkinter import asksaveasfilename, askopenfilename
  3. import tkinter.messagebox
  4. from draftboard import drawingfunction
  5. SCREEN = None
  6. pen_weight_input = None
  7. span_input = None
  8. background_image = None
  9. func_logger = None
  10. coordinate_system_drawing_method = None
  11. background_color = None
  12. pen_weight = None
  13. pen_color = None
  14. increasing_color = None
  15. subtraction_color = None
  16. save_dir = None
  17. gui_width = 20
  18. gui_height = 3
  19. span = None
  20. bg_color = "#FFFAFA" # 主颜色
  21. botton_color = "#FFFAFA" # 按钮颜色
  22. word_color = "#000000" # 文字颜色
  23. help_doc = """
  24. *快捷键:
  25. d-不用点击左键画线(再次点击关闭)
  26. g-画直线(2)
  27. f-画填充矩阵3
  28. s-画矩阵边框4
  29. k-画横线5
  30. l-画竖线6
  31. j-画多段线7
  32. i-横线多段线打点(再次点击结束绘制)8
  33. u-竖线多段线打点(再次点击结束绘制)9
  34. h-横线和竖线多段线打点并由虚线标注(再次点击结束绘制)10
  35. q-绘制虚线11
  36. c-绘制填充圆形12
  37. v-绘制圆形边框(13)
  38. n和m-绘制多边形(14)
  39. n-再次点击完成填充多边形绘制(14)
  40. m-再次点击完成多边形边框绘制(14)
  41. o-捕捉坐标原点(请先点击功能快捷键)
  42. x-捕捉坐标x轴(请先点击功能快捷键并选择起点)
  43. y-捕捉坐标y轴(同上)
  44. b-关闭当前所有快捷键操作
  45. e-绘制填充椭圆(15)
  46. r-绘制椭圆边框(16)
  47. w-再次保存(前提是现在工具箱点击过一次保存)
  48. *鼠标操作:
  49. 左键-按下画曲线
  50. 中键-快捷键启动后,用于选择点
  51. 右键-打开工具箱
  52. *视图
  53. 顶部-显示鼠标坐标,按键点击模式,笔的大小,坐标系位置和颜色系统以及坐标系原点的位置(处于快捷键状态时,显示中键当前选择了的点)
  54. 底部-显示当前时间(处于快捷键状态时,显示快捷键提示mod:yes None xxx,其中yes表示快捷键d启动,None表示快捷键注释,xxx表示快捷功能解释)
  55. *工具箱操作:
  56. 选择颜色-画线颜色
  57. 选择增函数颜色-绘制函数时增函数颜色
  58. 选择减函数颜色-绘制函数时减函数颜色
  59. 选择笔的大小(刷子)-画线的粗细
  60. 清空-清空所有笔记并可以选择新的背景颜色
  61. 绘制坐标系-三点绘制坐标系(使用中键选择点)
  62. 保存-保存笔记为图片格式,保存一次后快捷键w可重复保存
  63. 绘制函数-绘制基本初等函数和字定义解析函数
  64. """
  65. class UIAPI:
  66. @staticmethod
  67. def askok_gui(message):
  68. return tkinter.messagebox.askokcancel('提示', message)
  69. class API:
  70. @staticmethod
  71. def increasing_func_color():
  72. global increasing_color
  73. increasing_color = askcolor(title="选择颜色")[0]
  74. @staticmethod
  75. def subtraction_func_color():
  76. global subtraction_color
  77. subtraction_color = askcolor(title="选择颜色")[0]
  78. @staticmethod
  79. def select_color():
  80. global pen_color
  81. pen_color = askcolor(title="选择颜色")[0]
  82. @staticmethod
  83. def choose_save():
  84. global save_dir
  85. save_dir = asksaveasfilename(
  86. title="选择保存位置", filetypes=[("PNG", ".png")]
  87. )
  88. if not save_dir:
  89. save_dir = None
  90. else:
  91. save_dir += ".png"
  92. @staticmethod
  93. def choose_open():
  94. global background_image
  95. background_image = askopenfilename(
  96. title="选择载入图片", filetypes=[("PNG", ".png"), ("JPG", ".jpg")]
  97. )
  98. if not background_image:
  99. background_image = None
  100. @staticmethod
  101. def switch_brush():
  102. global pen_weight
  103. if UIAPI.askok_gui("要切换到刷子吗(可当橡皮使用)"):
  104. pen_weight = 10
  105. @staticmethod
  106. def switch_big():
  107. global pen_weight
  108. if UIAPI.askok_gui("要切换到大笔吗"):
  109. pen_weight = 3
  110. @staticmethod
  111. def set_pen():
  112. global pen_weight, pen_weight_input
  113. pen = pen_weight_input.get().replace(" ", "")
  114. try:
  115. pen = int(pen)
  116. if UIAPI.askok_gui(f"是否设定大小为{pen}(系统默认大小为:2)"):
  117. pen_weight = pen
  118. except BaseException:
  119. if UIAPI.askok_gui("设置失败,是否要切换到中笔吗"):
  120. pen_weight = 2
  121. @staticmethod
  122. def switch_stroke():
  123. global pen_weight
  124. if UIAPI.askok_gui("要切换到中笔吗"):
  125. pen_weight = 2
  126. @staticmethod
  127. def switch_small():
  128. global pen_weight
  129. if UIAPI.askok_gui("要切换到小笔吗?"):
  130. pen_weight = 1
  131. @staticmethod
  132. def plot_coordinate():
  133. global coordinate_system_drawing_method
  134. if tkinter.messagebox.askokcancel(
  135. "提示", "是否绘制坐标系,确定后返回草图界面任一点三点开始绘制(点击取消可撤销未执行的清空)"
  136. ):
  137. coordinate_system_drawing_method = 1
  138. else:
  139. coordinate_system_drawing_method = None
  140. @staticmethod
  141. def plot_coordinate_small():
  142. global coordinate_system_drawing_method
  143. if tkinter.messagebox.askokcancel(
  144. "提示", "是否绘制小跨度的坐标系,确定后返回草图界面任一点三点开始绘制(点击取消可撤销未执行的清空)"
  145. ):
  146. coordinate_system_drawing_method = 2
  147. else:
  148. coordinate_system_drawing_method = None
  149. @staticmethod
  150. def plot_coordinate_big_span():
  151. global coordinate_system_drawing_method
  152. if tkinter.messagebox.askokcancel(
  153. "提示", "是否绘制大跨度的坐标系,确定后返回草图界面任一点三点开始绘制(点击取消可撤销未执行的清空)"
  154. ):
  155. coordinate_system_drawing_method = 3
  156. else:
  157. coordinate_system_drawing_method = None
  158. @staticmethod
  159. def set_span():
  160. global coordinate_system_drawing_method, span, span_input
  161. span_input_str = span_input.get().replace(" ", "")
  162. try:
  163. span_input_str = int(span_input_str)
  164. if tkinter.messagebox.askokcancel(
  165. "提示", f"是否设定跨度为{span_input_str}(跨度代表坐标系一个单位大小的实际像素,系统默认大跨度为:120)"
  166. ):
  167. span = span_input_str
  168. coordinate_system_drawing_method = 1
  169. else:
  170. coordinate_system_drawing_method = None
  171. span = None
  172. except BaseException:
  173. span = None
  174. if tkinter.messagebox.askokcancel(
  175. "提示", "是否绘制大跨度的坐标系,确定后返回草图界面任一点三点开始绘制(点击取消可撤销未执行的清空)"
  176. ):
  177. coordinate_system_drawing_method = 3
  178. else:
  179. coordinate_system_drawing_method = None
  180. @staticmethod
  181. def empty():
  182. global background_color
  183. if UIAPI.askok_gui("是否清空草稿(点击取消可撤销未执行的清空)"):
  184. background_color = askcolor(title="选择背景颜色")[0]
  185. else:
  186. background_color = None
  187. @staticmethod
  188. def open_func_box():
  189. global func_logger
  190. func_logger = drawingfunction.func_box()
  191. @staticmethod
  192. def show_help():
  193. global help_doc
  194. tkinter.messagebox.showinfo(title="帮助", message=help_doc)
  195. def tool_box():
  196. global SCREEN, span_input # 初始化屏幕
  197. SCREEN = tkinter.Tk() # 设置屏幕
  198. SCREEN["bg"] = bg_color
  199. SCREEN.title("Tool")
  200. SCREEN.resizable(width=False, height=False)
  201. SCREEN.geometry(f"+10+10")
  202. tkinter.Button(
  203. SCREEN,
  204. text="选择颜色",
  205. bg=bg_color,
  206. fg=word_color,
  207. command=API.select_color,
  208. width=gui_width,
  209. height=gui_height,
  210. ).pack()
  211. tkinter.Button(
  212. SCREEN,
  213. text="选择增函数颜色",
  214. bg=bg_color,
  215. fg=word_color,
  216. command=API.increasing_func_color,
  217. width=gui_width,
  218. height=1,
  219. ).pack()
  220. tkinter.Button(
  221. SCREEN,
  222. text="选择减函数颜色",
  223. bg=bg_color,
  224. fg=word_color,
  225. command=API.subtraction_func_color,
  226. width=gui_width,
  227. height=1,
  228. ).pack()
  229. tkinter.Button(
  230. SCREEN,
  231. text="使用中笔(默认笔)",
  232. bg=bg_color,
  233. fg=word_color,
  234. command=API.switch_stroke,
  235. width=gui_width,
  236. height=gui_height,
  237. ).pack()
  238. tkinter.Button(
  239. SCREEN,
  240. text="使用大笔",
  241. bg=bg_color,
  242. fg=word_color,
  243. command=API.switch_big,
  244. width=gui_width,
  245. height=1,
  246. ).pack() # 切换到大笔
  247. tkinter.Button(
  248. SCREEN,
  249. text="使用小笔",
  250. bg=bg_color,
  251. fg=word_color,
  252. command=API.switch_small,
  253. width=gui_width,
  254. height=1,
  255. ).pack() # 切换笔
  256. tkinter.Button(
  257. SCREEN,
  258. text="使用刷子",
  259. bg=bg_color,
  260. fg=word_color,
  261. command=API.switch_brush,
  262. width=gui_width,
  263. height=1,
  264. ).pack() # 切换笔
  265. pen_weight_input = tkinter.Entry(SCREEN, width=gui_width - 2)
  266. pen_weight_input.pack(fill=tkinter.BOTH)
  267. tkinter.Button(
  268. SCREEN,
  269. text="使用自定义大小",
  270. bg=bg_color,
  271. fg=word_color,
  272. command=API.set_pen,
  273. width=gui_width,
  274. height=1,
  275. ).pack() # 切换笔
  276. tkinter.Button(
  277. SCREEN,
  278. text="清空草稿",
  279. bg=bg_color,
  280. fg=word_color,
  281. command=API.empty,
  282. width=gui_width,
  283. height=gui_height,
  284. ).pack() # 填充背景
  285. tkinter.Button(
  286. SCREEN,
  287. text="绘制坐标系",
  288. bg=bg_color,
  289. fg=word_color,
  290. command=API.plot_coordinate,
  291. width=gui_width,
  292. height=gui_height,
  293. ).pack()
  294. tkinter.Button(
  295. SCREEN,
  296. text="绘制坐标系(小跨度)",
  297. bg=bg_color,
  298. fg=word_color,
  299. command=API.plot_coordinate_small,
  300. width=gui_width,
  301. height=1,
  302. ).pack()
  303. tkinter.Button(
  304. SCREEN,
  305. text="绘制坐标系(大跨度)",
  306. command=API.plot_coordinate_big_span,
  307. width=gui_width,
  308. height=1,
  309. bg=bg_color,
  310. fg=word_color,
  311. ).pack() # 绘制坐标系
  312. span_input = tkinter.Entry(SCREEN, width=gui_width - 2)
  313. span_input.pack(fill=tkinter.BOTH)
  314. tkinter.Button(
  315. SCREEN,
  316. text="使用自定义跨度",
  317. bg=bg_color,
  318. fg=word_color,
  319. command=API.set_span,
  320. width=gui_width,
  321. height=1,
  322. ).pack() # 切换笔
  323. tkinter.Button(
  324. SCREEN,
  325. text="绘制函数",
  326. bg=bg_color,
  327. fg=word_color,
  328. command=API.open_func_box,
  329. width=gui_width,
  330. height=gui_height,
  331. ).pack()
  332. tkinter.Button(
  333. SCREEN,
  334. text="保存",
  335. bg=bg_color,
  336. fg=word_color,
  337. command=API.choose_save,
  338. width=gui_width,
  339. height=1,
  340. ).pack()
  341. tkinter.Button(
  342. SCREEN,
  343. text="载入",
  344. bg=bg_color,
  345. fg=word_color,
  346. command=API.choose_open,
  347. width=gui_width,
  348. height=1,
  349. ).pack()
  350. tkinter.Button(
  351. SCREEN,
  352. text="帮助",
  353. bg=bg_color,
  354. fg=word_color,
  355. command=API.show_help,
  356. width=gui_width,
  357. height=1,
  358. ).pack() # help是系统保留关键词,用_help代替
  359. SCREEN.mainloop()
  360. try:
  361. func = func_logger()
  362. except BaseException:
  363. func = {}
  364. return [
  365. pen_color,
  366. pen_weight,
  367. background_color,
  368. coordinate_system_drawing_method,
  369. func,
  370. save_dir,
  371. increasing_color,
  372. subtraction_color,
  373. span,
  374. background_image,
  375. ]