toolbox.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 = drawingfunction.Logger()
  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. except ValueError:
  117. if UIAPI.askok_gui("设置失败,是否要切换到中笔吗"):
  118. pen_weight = 2
  119. else:
  120. if UIAPI.askok_gui(f"是否设定大小为{pen}(系统默认大小为:2)"):
  121. pen_weight = pen
  122. @staticmethod
  123. def switch_stroke():
  124. global pen_weight
  125. if UIAPI.askok_gui("要切换到中笔吗"):
  126. pen_weight = 2
  127. @staticmethod
  128. def switch_small():
  129. global pen_weight
  130. if UIAPI.askok_gui("要切换到小笔吗?"):
  131. pen_weight = 1
  132. @staticmethod
  133. def plot_coordinate():
  134. global coordinate_system_drawing_method
  135. if tkinter.messagebox.askokcancel(
  136. "提示", "是否绘制坐标系,确定后返回草图界面任一点三点开始绘制(点击取消可撤销未执行的清空)"
  137. ):
  138. coordinate_system_drawing_method = 1
  139. else:
  140. coordinate_system_drawing_method = None
  141. @staticmethod
  142. def plot_coordinate_small():
  143. global coordinate_system_drawing_method
  144. if tkinter.messagebox.askokcancel(
  145. "提示", "是否绘制小跨度的坐标系,确定后返回草图界面任一点三点开始绘制(点击取消可撤销未执行的清空)"
  146. ):
  147. coordinate_system_drawing_method = 2
  148. else:
  149. coordinate_system_drawing_method = None
  150. @staticmethod
  151. def plot_coordinate_big_span():
  152. global coordinate_system_drawing_method
  153. if tkinter.messagebox.askokcancel(
  154. "提示", "是否绘制大跨度的坐标系,确定后返回草图界面任一点三点开始绘制(点击取消可撤销未执行的清空)"
  155. ):
  156. coordinate_system_drawing_method = 3
  157. else:
  158. coordinate_system_drawing_method = None
  159. @staticmethod
  160. def set_span():
  161. global coordinate_system_drawing_method, span, span_input
  162. span_input_str = span_input.get().replace(" ", "")
  163. try:
  164. span_input_str = int(span_input_str)
  165. except ValueError:
  166. span = None
  167. if tkinter.messagebox.askokcancel(
  168. "提示", "是否绘制大跨度的坐标系,确定后返回草图界面任一点三点开始绘制(点击取消可撤销未执行的清空)"
  169. ):
  170. coordinate_system_drawing_method = 3
  171. else:
  172. coordinate_system_drawing_method = None
  173. else:
  174. if tkinter.messagebox.askokcancel(
  175. "提示", f"是否设定跨度为{span_input_str}(跨度代表坐标系一个单位大小的实际像素,系统默认大跨度为:120)"
  176. ):
  177. span = span_input_str
  178. coordinate_system_drawing_method = 1
  179. else:
  180. coordinate_system_drawing_method = None
  181. span = None
  182. @staticmethod
  183. def empty():
  184. global background_color
  185. if UIAPI.askok_gui("是否清空草稿(点击取消可撤销未执行的清空)"):
  186. background_color = askcolor(title="选择背景颜色")[0]
  187. else:
  188. background_color = None
  189. @staticmethod
  190. def open_func_box():
  191. global func_logger
  192. func_logger = drawingfunction.func_box()
  193. @staticmethod
  194. def show_help():
  195. global help_doc
  196. tkinter.messagebox.showinfo(title="帮助", message=help_doc)
  197. def tool_box():
  198. global SCREEN, span_input, pen_weight_input # 初始化屏幕
  199. SCREEN = tkinter.Tk() # 设置屏幕
  200. SCREEN["bg"] = bg_color
  201. SCREEN.title("Tool")
  202. SCREEN.resizable(width=False, height=False)
  203. SCREEN.geometry(f"+10+10")
  204. tkinter.Button(
  205. SCREEN,
  206. text="选择颜色",
  207. bg=bg_color,
  208. fg=word_color,
  209. command=API.select_color,
  210. width=gui_width,
  211. height=gui_height,
  212. ).pack()
  213. tkinter.Button(
  214. SCREEN,
  215. text="选择增函数颜色",
  216. bg=bg_color,
  217. fg=word_color,
  218. command=API.increasing_func_color,
  219. width=gui_width,
  220. height=1,
  221. ).pack()
  222. tkinter.Button(
  223. SCREEN,
  224. text="选择减函数颜色",
  225. bg=bg_color,
  226. fg=word_color,
  227. command=API.subtraction_func_color,
  228. width=gui_width,
  229. height=1,
  230. ).pack()
  231. tkinter.Button(
  232. SCREEN,
  233. text="使用中笔(默认笔)",
  234. bg=bg_color,
  235. fg=word_color,
  236. command=API.switch_stroke,
  237. width=gui_width,
  238. height=gui_height,
  239. ).pack()
  240. tkinter.Button(
  241. SCREEN,
  242. text="使用大笔",
  243. bg=bg_color,
  244. fg=word_color,
  245. command=API.switch_big,
  246. width=gui_width,
  247. height=1,
  248. ).pack() # 切换到大笔
  249. tkinter.Button(
  250. SCREEN,
  251. text="使用小笔",
  252. bg=bg_color,
  253. fg=word_color,
  254. command=API.switch_small,
  255. width=gui_width,
  256. height=1,
  257. ).pack() # 切换笔
  258. tkinter.Button(
  259. SCREEN,
  260. text="使用刷子",
  261. bg=bg_color,
  262. fg=word_color,
  263. command=API.switch_brush,
  264. width=gui_width,
  265. height=1,
  266. ).pack() # 切换笔
  267. pen_weight_input = tkinter.Entry(SCREEN, width=gui_width - 2)
  268. pen_weight_input.pack(fill=tkinter.BOTH)
  269. tkinter.Button(
  270. SCREEN,
  271. text="使用自定义大小",
  272. bg=bg_color,
  273. fg=word_color,
  274. command=API.set_pen,
  275. width=gui_width,
  276. height=1,
  277. ).pack() # 切换笔
  278. tkinter.Button(
  279. SCREEN,
  280. text="清空草稿",
  281. bg=bg_color,
  282. fg=word_color,
  283. command=API.empty,
  284. width=gui_width,
  285. height=gui_height,
  286. ).pack() # 填充背景
  287. tkinter.Button(
  288. SCREEN,
  289. text="绘制坐标系",
  290. bg=bg_color,
  291. fg=word_color,
  292. command=API.plot_coordinate,
  293. width=gui_width,
  294. height=gui_height,
  295. ).pack()
  296. tkinter.Button(
  297. SCREEN,
  298. text="绘制坐标系(小跨度)",
  299. bg=bg_color,
  300. fg=word_color,
  301. command=API.plot_coordinate_small,
  302. width=gui_width,
  303. height=1,
  304. ).pack()
  305. tkinter.Button(
  306. SCREEN,
  307. text="绘制坐标系(大跨度)",
  308. command=API.plot_coordinate_big_span,
  309. width=gui_width,
  310. height=1,
  311. bg=bg_color,
  312. fg=word_color,
  313. ).pack() # 绘制坐标系
  314. span_input = tkinter.Entry(SCREEN, width=gui_width - 2)
  315. span_input.pack(fill=tkinter.BOTH)
  316. tkinter.Button(
  317. SCREEN,
  318. text="使用自定义跨度",
  319. bg=bg_color,
  320. fg=word_color,
  321. command=API.set_span,
  322. width=gui_width,
  323. height=1,
  324. ).pack() # 切换笔
  325. tkinter.Button(
  326. SCREEN,
  327. text="绘制函数",
  328. bg=bg_color,
  329. fg=word_color,
  330. command=API.open_func_box,
  331. width=gui_width,
  332. height=gui_height,
  333. ).pack()
  334. tkinter.Button(
  335. SCREEN,
  336. text="保存",
  337. bg=bg_color,
  338. fg=word_color,
  339. command=API.choose_save,
  340. width=gui_width,
  341. height=1,
  342. ).pack()
  343. tkinter.Button(
  344. SCREEN,
  345. text="载入",
  346. bg=bg_color,
  347. fg=word_color,
  348. command=API.choose_open,
  349. width=gui_width,
  350. height=1,
  351. ).pack()
  352. tkinter.Button(
  353. SCREEN,
  354. text="帮助",
  355. bg=bg_color,
  356. fg=word_color,
  357. command=API.show_help,
  358. width=gui_width,
  359. height=1,
  360. ).pack() # help是系统保留关键词,用_help代替
  361. SCREEN.mainloop()
  362. func = func_logger()
  363. return [
  364. pen_color,
  365. pen_weight,
  366. background_color,
  367. coordinate_system_drawing_method,
  368. func,
  369. save_dir,
  370. increasing_color,
  371. subtraction_color,
  372. span,
  373. background_image,
  374. ]