gui.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import tkinter
  2. from newtkinter import askopenfilename
  3. from tkinter.messagebox import showwarning, askokcancel
  4. from tkinter.scrolledtext import ScrolledText
  5. from system.controller import Plugin, NamingError, ConflictError
  6. SCREEN = tkinter.Tk()
  7. plugin = Plugin()
  8. SCREEN.title("插件管理")
  9. SCREEN.resizable(width=False, height=False)
  10. SCREEN.geometry(f"+10+10")
  11. bg_color = "#FFFAFA" # 主颜色
  12. SCREEN["bg"] = bg_color
  13. botton_color = "#FFFAFA" # 按钮颜色
  14. word_color = "#000000" # 文字颜色
  15. FONT = ("黑体", 11) # 设置字体
  16. gui_width = 13 # 标准宽度
  17. gui_height = 2
  18. row = 0
  19. column = 0
  20. def code_window(name):
  21. global bg_color
  22. cli_screen = tkinter.Toplevel(bg=bg_color)
  23. cli_screen.title(f"插件查看器:{name}")
  24. cli_screen.geometry("+10+10") # 设置所在位置
  25. cli_screen.resizable(width=False, height=False)
  26. class ScrolledCli(ScrolledText):
  27. def __init__(self, *args, **kwargs):
  28. super(ScrolledCli, self).__init__(*args, **kwargs)
  29. def insert(self, index, chars, *args):
  30. text.config(state=tkinter.NORMAL)
  31. super(ScrolledCli, self).insert(index, chars, *args)
  32. text.config(state=tkinter.DISABLED)
  33. def clear(self):
  34. text.config(state=tkinter.NORMAL)
  35. self.delete("0.0", tkinter.END)
  36. text.config(state=tkinter.DISABLED)
  37. text = ScrolledCli(cli_screen, font=("黑体", 11), height=30, width=100)
  38. text.grid(column=0, row=0, columnspan=5, sticky=tkinter.E + tkinter.W)
  39. text.config(state=tkinter.DISABLED)
  40. cli_screen.update()
  41. return text, cli_screen
  42. def get_dir():
  43. plugin_dir_box.delete(0, tkinter.END)
  44. plugin_dir_box.insert(0, *plugin.get_dir())
  45. def get_all_plugin():
  46. plugin_box.delete(0, tkinter.END)
  47. plugin_box.insert(0, *plugin.get_all_plugin())
  48. def get_plugin():
  49. try:
  50. index = plugin_dir_box.curselection()[0]
  51. except IndexError:
  52. return False
  53. plugin_box.delete(0, tkinter.END)
  54. plugin_box.insert(0, *plugin.get_plugin(index))
  55. def add_plugin():
  56. index = plugin_dir_box.curselection()[0]
  57. plugin_dir = askopenfilename(title="选择插件文件", filetypes=[("Python", ".py")])
  58. try:
  59. plugin_list = plugin.add_plugin(index, plugin_dir)
  60. except NamingError:
  61. showwarning("文件错误", "插件命名错误,命名规则:\ntemplate_[类\\方法名].py")
  62. except ConflictError:
  63. if askokcancel("提示", f"已经存在插件,是否需要尝试合并插件?\n[合并失败将产生不可逆的后果]"):
  64. plugin.merge_plugin(index, plugin_dir)
  65. except BaseException:
  66. showwarning("文件错误", "插件导入遇到了未知错误")
  67. else:
  68. plugin_box.delete(0, tkinter.END)
  69. plugin_box.insert(0, *plugin_list)
  70. def del_plugin():
  71. index = plugin_box.curselection()[0]
  72. try:
  73. plugin_list = plugin.del_plugin(index)
  74. except BaseException:
  75. pass
  76. else:
  77. plugin_box.delete(0, tkinter.END)
  78. plugin_box.insert(0, *plugin_list)
  79. def show_plugin():
  80. index = plugin_box.curselection()[0]
  81. try:
  82. code, name = plugin.show_plugin(index)
  83. except BaseException:
  84. pass
  85. else:
  86. code_window(name)[0].insert(tkinter.END, code)
  87. def system_main():
  88. global SCREEN
  89. SCREEN.mainloop()
  90. (
  91. tkinter.Label(
  92. SCREEN,
  93. text="【插件管理】",
  94. bg=bg_color,
  95. fg=word_color,
  96. font=FONT,
  97. width=gui_width * 3,
  98. height=gui_height,
  99. ).grid(column=column, row=row, columnspan=3)
  100. )
  101. row += 1
  102. (
  103. tkinter.Button(
  104. SCREEN,
  105. bg=botton_color,
  106. fg=word_color,
  107. command=get_dir,
  108. text="查看插件列表",
  109. font=FONT,
  110. width=gui_width,
  111. height=gui_height,
  112. ).grid(column=column, row=row, sticky=tkinter.E + tkinter.W)
  113. )
  114. (
  115. tkinter.Button(
  116. SCREEN,
  117. bg=botton_color,
  118. fg=word_color,
  119. command=get_all_plugin,
  120. text="查看所有插件",
  121. font=FONT,
  122. width=gui_width,
  123. height=gui_height,
  124. ).grid(column=column + 1, row=row, sticky=tkinter.E + tkinter.W)
  125. )
  126. (
  127. tkinter.Button(
  128. SCREEN,
  129. bg=botton_color,
  130. fg=word_color,
  131. command=get_plugin,
  132. text="查看仓库插件",
  133. font=FONT,
  134. width=gui_width,
  135. height=gui_height,
  136. ).grid(column=column + 2, row=row, sticky=tkinter.E + tkinter.W)
  137. )
  138. row += 1
  139. plugin_box = tkinter.Listbox(SCREEN, width=gui_width * 3, height=gui_height * 5)
  140. plugin_box.grid(
  141. column=column,
  142. row=row,
  143. columnspan=3,
  144. rowspan=5,
  145. sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N,
  146. )
  147. row += 5
  148. (
  149. tkinter.Label(
  150. SCREEN,
  151. text="【插件仓库】",
  152. bg=bg_color,
  153. fg=word_color,
  154. font=FONT,
  155. width=gui_width * 3,
  156. height=gui_height,
  157. ).grid(column=column, row=row, columnspan=3)
  158. )
  159. row += 1
  160. plugin_dir_box = tkinter.Listbox(SCREEN, width=gui_width * 3, height=gui_height * 5)
  161. plugin_dir_box.grid(
  162. column=column,
  163. row=row,
  164. columnspan=3,
  165. rowspan=5,
  166. sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N,
  167. )
  168. row += 5
  169. (
  170. tkinter.Button(
  171. SCREEN,
  172. bg=botton_color,
  173. fg=word_color,
  174. command=add_plugin,
  175. text="新增插件",
  176. font=FONT,
  177. width=gui_width,
  178. height=gui_height,
  179. ).grid(column=column, row=row, sticky=tkinter.E + tkinter.W)
  180. )
  181. (
  182. tkinter.Button(
  183. SCREEN,
  184. bg=botton_color,
  185. fg=word_color,
  186. command=del_plugin,
  187. text="删除插件",
  188. font=FONT,
  189. width=gui_width,
  190. height=gui_height,
  191. ).grid(column=column + 1, row=row, sticky=tkinter.E + tkinter.W)
  192. )
  193. (
  194. tkinter.Button(
  195. SCREEN,
  196. bg=botton_color,
  197. fg=word_color,
  198. command=show_plugin,
  199. text="查看插件",
  200. font=FONT,
  201. width=gui_width,
  202. height=gui_height,
  203. ).grid(column=column + 2, row=row, sticky=tkinter.E + tkinter.W)
  204. )