gui.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import tkinter
  2. from tkinter.filedialog 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. (tkinter.Label(SCREEN, text='【插件管理】', bg=bg_color, fg=word_color, font=FONT, width=gui_width*3, height=gui_height)
  91. .grid(column=column, row=row, columnspan=3))
  92. row += 1
  93. (tkinter.Button(SCREEN, bg=botton_color, fg=word_color, command=get_dir, text='查看插件列表', font=FONT, width=gui_width, height=gui_height)
  94. .grid(column=column, row=row, sticky=tkinter.E + tkinter.W))
  95. (tkinter.Button(SCREEN, bg=botton_color, fg=word_color, command=get_all_plugin, text='查看所有插件', font=FONT, width=gui_width, height=gui_height)
  96. .grid(column=column + 1, row=row, sticky=tkinter.E + tkinter.W))
  97. (tkinter.Button(SCREEN, bg=botton_color, fg=word_color, command=get_plugin, text='查看仓库插件', font=FONT, width=gui_width, height=gui_height)
  98. .grid(column=column + 2, row=row, sticky=tkinter.E + tkinter.W))
  99. row += 1
  100. plugin_box = tkinter.Listbox(SCREEN, width=gui_width * 3, height=gui_height * 5)
  101. plugin_box.grid(column=column, row=row, columnspan=3, rowspan=5, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  102. row += 5
  103. (tkinter.Label(SCREEN, text='【插件仓库】', bg=bg_color, fg=word_color, font=FONT, width=gui_width*3, height=gui_height)
  104. .grid(column=column, row=row, columnspan=3))
  105. row += 1
  106. plugin_dir_box = tkinter.Listbox(SCREEN, width=gui_width * 3, height=gui_height * 5)
  107. plugin_dir_box.grid(column=column, row=row, columnspan=3, rowspan=5, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  108. row += 5
  109. (tkinter.Button(SCREEN, bg=botton_color, fg=word_color, command=add_plugin, text='新增插件', font=FONT, width=gui_width, height=gui_height)
  110. .grid(column=column, row=row, sticky=tkinter.E + tkinter.W))
  111. (tkinter.Button(SCREEN, bg=botton_color, fg=word_color, command=del_plugin, text='删除插件', font=FONT, width=gui_width, height=gui_height)
  112. .grid(column=column + 1, row=row, sticky=tkinter.E + tkinter.W))
  113. (tkinter.Button(SCREEN, bg=botton_color, fg=word_color, command=show_plugin, text='查看插件', font=FONT, width=gui_width, height=gui_height)
  114. .grid(column=column + 2, row=row, sticky=tkinter.E + tkinter.W))