drawingfunction.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. import math
  2. import tkinter.messagebox
  3. from draftboard import customfunctions
  4. custom_function_index = 18 # 字定义函数的序号
  5. func_dict = {}
  6. custom_func_dict = {}
  7. width = 20
  8. SCREEN = None # 设置屏幕
  9. bg_color = "#FFFAFA" # 主颜色
  10. botton_color = "#FFFAFA" # 按钮颜色
  11. word_color = "#000000" # 文字颜色
  12. class Logger:
  13. def __call__(self, *args, **kwargs):
  14. global custom_func_dict, func_dict
  15. for i in custom_func_dict:
  16. func_dict[i] = custom_func_dict[i]()
  17. return func_dict
  18. class UIAPI:
  19. @staticmethod
  20. def askok_gui(message):
  21. return tkinter.messagebox.askokcancel('提示', message)
  22. class API:
  23. @staticmethod
  24. def linear_func():
  25. global func_dict
  26. if UIAPI.askok_gui("是否绘制1次函数(点击取消可撤销未执行的函数)"):
  27. func_dict[1] = lambda x: x
  28. else:
  29. func_dict[1] = None
  30. @staticmethod
  31. def quadratic_func():
  32. global func_dict
  33. if UIAPI.askok_gui("是否绘制2次函数(点击取消可撤销未执行的函数)"):
  34. func_dict[2] = lambda x: x ** 2
  35. else:
  36. func_dict[2] = None
  37. @staticmethod
  38. def cubic_function():
  39. global func_dict
  40. if UIAPI.askok_gui("是否绘制3次函数(点击取消可撤销未执行的函数)"):
  41. func_dict[4] = lambda x: x ** 3
  42. else:
  43. func_dict[4] = None
  44. @staticmethod
  45. def inverse_func():
  46. global func_dict
  47. if UIAPI.askok_gui("是否绘制-1次函数(点击取消可撤销未执行的函数)"):
  48. func_dict[3] = lambda x: 1 / x
  49. else:
  50. func_dict[3] = None
  51. @staticmethod
  52. def radical_func():
  53. global func_dict
  54. if UIAPI.askok_gui("是否绘制根号函数(点击取消可撤销未执行的函数)"):
  55. func_dict[5] = lambda x: x ** (1 / 2)
  56. else:
  57. func_dict[5] = None
  58. @staticmethod
  59. def exp_func():
  60. global func_dict
  61. if UIAPI.askok_gui("是否绘制指数函数(点击取消可撤销未执行的函数)"):
  62. func_dict[6] = lambda x: 10 ** x
  63. else:
  64. func_dict[6] = None
  65. @staticmethod
  66. def log_func():
  67. global func_dict
  68. if UIAPI.askok_gui("是否绘制对数函数(点击取消可撤销未执行的函数)"):
  69. func_dict[7] = lambda x: math.log(x, 2)
  70. else:
  71. func_dict[7] = None
  72. @staticmethod
  73. def log2_func():
  74. global func_dict
  75. if UIAPI.askok_gui("是否绘制对数函数2(点击取消可撤销未执行的函数)"):
  76. func_dict[8] = lambda x: math.log(2, x)
  77. else:
  78. func_dict[8] = None
  79. @staticmethod
  80. def sin_func():
  81. global func_dict
  82. if UIAPI.askok_gui("是否绘制正弦函数(点击取消可撤销未执行的函数)"):
  83. func_dict[9] = lambda x: math.sin(x)
  84. else:
  85. func_dict[9] = None
  86. @staticmethod
  87. def cos_func():
  88. global func_dict
  89. if UIAPI.askok_gui("是否绘制余弦函数(点击取消可撤销未执行的函数)"):
  90. func_dict[10] = lambda x: math.cos(x)
  91. else:
  92. func_dict[10] = None
  93. @staticmethod
  94. def tan_func():
  95. global func_dict
  96. if UIAPI.askok_gui("是否绘制正切函数(点击取消可撤销未执行的函数)"):
  97. func_dict[11] = lambda x: math.tan(x)
  98. else:
  99. func_dict[11] = None
  100. @staticmethod
  101. def cot_func():
  102. global func_dict
  103. if UIAPI.askok_gui("是否绘制余切函数(点击取消可撤销未执行的函数)"):
  104. func_dict[12] = lambda x: 1 / math.tan(x)
  105. else:
  106. func_dict[12] = None
  107. @staticmethod
  108. def sec_func():
  109. global func_dict
  110. if UIAPI.askok_gui("是否绘制正割函数(点击取消可撤销未执行的函数)"):
  111. func_dict[13] = lambda x: 1 / math.cos(x)
  112. else:
  113. func_dict[13] = None
  114. @staticmethod
  115. def csc_func():
  116. global func_dict
  117. if UIAPI.askok_gui("是否绘制余割函数(点击取消可撤销未执行的函数)"):
  118. func_dict[11] = lambda x: 1 / math.sin(x)
  119. else:
  120. func_dict[11] = None
  121. @staticmethod
  122. def arcsin_func():
  123. global func_dict
  124. if UIAPI.askok_gui("是否绘制反正弦函数(点击取消可撤销未执行的函数)"):
  125. func_dict[12] = lambda x: math.asin(x)
  126. else:
  127. func_dict[12] = None
  128. @staticmethod
  129. def arccos_func():
  130. global func_dict
  131. if UIAPI.askok_gui("是否绘制反余弦函数(点击取消可撤销未执行的函数)"):
  132. func_dict[13] = lambda x: math.acos(x)
  133. else:
  134. func_dict[13] = None
  135. @staticmethod
  136. def arctan_func():
  137. global func_dict
  138. if UIAPI.askok_gui("是否绘制反正切函数(点击取消可撤销未执行的函数)"):
  139. func_dict[14] = lambda x: math.atan(x)
  140. else:
  141. func_dict[14] = None
  142. @staticmethod
  143. def arccot_func():
  144. global func_dict
  145. if UIAPI.askok_gui("是否绘制反余切函数(点击取消可撤销未执行的函数)"):
  146. func_dict[15] = lambda x: 1 / math.atan(x)
  147. else:
  148. func_dict[15] = None
  149. @staticmethod
  150. def arcsec_func():
  151. global func_dict
  152. if UIAPI.askok_gui("是否绘制反正割函数(点击取消可撤销未执行的函数)"):
  153. func_dict[16] = lambda x: 1 / math.acos(x)
  154. else:
  155. func_dict[16] = None
  156. @staticmethod
  157. def arccsc_func():
  158. global func_dict
  159. if UIAPI.askok_gui("是否绘制反余割函数(点击取消可撤销未执行的函数)"):
  160. func_dict[17] = lambda x: 1 / math.asin(x)
  161. else:
  162. func_dict[17] = None
  163. @staticmethod
  164. def custom_func():
  165. global func_dict, custom_function_index, custom_func_dict
  166. custom_func_dict[custom_function_index] = customfunctions.make_func()
  167. def func_box():
  168. global SCREEN
  169. loger = Logger()
  170. SCREEN = tkinter.Toplevel(bg=bg_color) # 设置屏幕
  171. SCREEN.title("")
  172. SCREEN.resizable(width=False, height=False)
  173. SCREEN.geometry(f"+180+10")
  174. tkinter.Button(
  175. SCREEN,
  176. text="1次函数",
  177. bg=bg_color,
  178. fg=word_color,
  179. command=API.linear_func,
  180. width=width,
  181. height=1,
  182. ).pack()
  183. tkinter.Button(
  184. SCREEN,
  185. text="2次函数",
  186. bg=bg_color,
  187. fg=word_color,
  188. command=API.quadratic_func,
  189. width=width,
  190. height=1,
  191. ).pack()
  192. tkinter.Button(
  193. SCREEN,
  194. text="-1次函数",
  195. bg=bg_color,
  196. fg=word_color,
  197. command=API.inverse_func,
  198. width=width,
  199. height=1,
  200. ).pack()
  201. tkinter.Button(
  202. SCREEN,
  203. text="3次函数",
  204. bg=bg_color,
  205. fg=word_color,
  206. command=API.cubic_function,
  207. width=width,
  208. height=1,
  209. ).pack()
  210. tkinter.Button(
  211. SCREEN,
  212. text="根号函数",
  213. bg=bg_color,
  214. fg=word_color,
  215. command=API.radical_func,
  216. width=width,
  217. height=1,
  218. ).pack()
  219. tkinter.Button(
  220. SCREEN,
  221. text="对数函数",
  222. bg=bg_color,
  223. fg=word_color,
  224. command=API.log_func,
  225. width=width,
  226. height=1,
  227. ).pack()
  228. tkinter.Button(
  229. SCREEN,
  230. text="指数函数",
  231. bg=bg_color,
  232. fg=word_color,
  233. command=API.exp_func,
  234. width=width,
  235. height=1,
  236. ).pack()
  237. tkinter.Button(
  238. SCREEN,
  239. text="对数底函数",
  240. bg=bg_color,
  241. fg=word_color,
  242. command=API.log2_func,
  243. width=width,
  244. height=1,
  245. ).pack()
  246. tkinter.Button(
  247. SCREEN,
  248. text="sin函数",
  249. bg=bg_color,
  250. fg=word_color,
  251. command=API.sin_func,
  252. width=width,
  253. height=1,
  254. ).pack()
  255. tkinter.Button(
  256. SCREEN,
  257. text="cos函数",
  258. bg=bg_color,
  259. fg=word_color,
  260. command=API.cos_func,
  261. width=width,
  262. height=1,
  263. ).pack()
  264. tkinter.Button(
  265. SCREEN,
  266. text="tan函数",
  267. bg=bg_color,
  268. fg=word_color,
  269. command=API.tan_func,
  270. width=width,
  271. height=1,
  272. ).pack()
  273. tkinter.Button(
  274. SCREEN,
  275. text="cot函数",
  276. bg=bg_color,
  277. fg=word_color,
  278. command=API.tan_func,
  279. width=width,
  280. height=1,
  281. ).pack()
  282. tkinter.Button(
  283. SCREEN,
  284. text="csc函数",
  285. bg=bg_color,
  286. fg=word_color,
  287. command=API.csc_func,
  288. width=width,
  289. height=1,
  290. ).pack()
  291. tkinter.Button(
  292. SCREEN,
  293. text="sec函数",
  294. bg=bg_color,
  295. fg=word_color,
  296. command=API.sec_func,
  297. width=width,
  298. height=1,
  299. ).pack()
  300. tkinter.Button(
  301. SCREEN,
  302. text="arcsin函数",
  303. bg=bg_color,
  304. fg=word_color,
  305. command=API.arcsin_func,
  306. width=width,
  307. height=1,
  308. ).pack()
  309. tkinter.Button(
  310. SCREEN,
  311. text="arccos函数",
  312. bg=bg_color,
  313. fg=word_color,
  314. command=API.arccos_func,
  315. width=width,
  316. height=1,
  317. ).pack()
  318. tkinter.Button(
  319. SCREEN,
  320. text="arctan函数",
  321. bg=bg_color,
  322. fg=word_color,
  323. command=API.arctan_func,
  324. width=width,
  325. height=1,
  326. ).pack()
  327. tkinter.Button(
  328. SCREEN,
  329. text="arccot函数",
  330. bg=bg_color,
  331. fg=word_color,
  332. command=API.arccot_func,
  333. width=width,
  334. height=1,
  335. ).pack()
  336. tkinter.Button(
  337. SCREEN,
  338. text="arccsc函数",
  339. bg=bg_color,
  340. fg=word_color,
  341. command=API.arccsc_func,
  342. width=width,
  343. height=1,
  344. ).pack()
  345. tkinter.Button(
  346. SCREEN,
  347. text="arcsec函数",
  348. bg=bg_color,
  349. fg=word_color,
  350. command=API.arcsec_func,
  351. width=width,
  352. height=1,
  353. ).pack()
  354. tkinter.Button(
  355. SCREEN,
  356. text="自定义函数",
  357. bg=bg_color,
  358. fg=word_color,
  359. command=API.custom_func,
  360. width=width,
  361. height=3,
  362. ).pack()
  363. return loger