ui.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import tktool
  2. import tkinter
  3. from typing import List, Tuple, Callable, Optional
  4. import abc
  5. class HEnglishTkinter(tktool.TkEventMain, metaclass=abc.ABCMeta):
  6. tk_zoom = 1
  7. def set_after_run(self, ms, func, *args): # 正常运行前设置定时任务 super.__init__可能会调用
  8. self.init_after_run_list.append((ms, func, args))
  9. def __conf_set_after_run(self): # 配合 set_after_run 使用
  10. for ms, func, args in self.init_after_run_list:
  11. self.set_after_run_now(ms, func, *args)
  12. def set_after_run_now(self, ms, func, *args): # 正常运行时设置定时任务
  13. self._window.after(ms, func, *args)
  14. def __init__(self, title: str,
  15. top: Optional["HEnglishTkinter"] = None,
  16. size: Tuple[float, float] = ((1 / 3), (2 / 3))):
  17. self.init_after_run_list: List[Tuple[int, Callable, Tuple]] = []
  18. super(HEnglishTkinter, self).__init__()
  19. if top:
  20. self._window = tkinter.Toplevel(top._window)
  21. else:
  22. self._window = tkinter.Tk()
  23. self._sys_height = self._window.winfo_screenheight()
  24. self._sys_width = self._window.winfo_screenwidth()
  25. self._win_height = int(self._sys_height * size[1] * self.tk_zoom) # 窗口高度
  26. self._win_width = int(self._sys_width * size[0] * self.tk_zoom) # 窗口宽度
  27. self.__conf_windows(title)
  28. self.__conf_set_after_run()
  29. def __conf_windows(self, title: str):
  30. self._window.title(title)
  31. self._window.geometry(f'{self._win_width}x{self._win_height}')
  32. self._window['bg'] = '#F0FFFF'
  33. self._window.resizable(True, True) # 禁止缩放
  34. self._window.overrideredirect(False) # 显示标题栏
  35. self._window.bind('<Configure>', self.__window_resize) # 调整界面大小
  36. self._window.minsize(int(self._sys_width * (1 / 3) * self.tk_zoom),
  37. int(self._sys_height * (1 / 3) * self.tk_zoom))
  38. self._create_windows()
  39. self._set_windows()
  40. def __window_resize(self, event=None):
  41. if self._win_width != self._window.winfo_width() or self._win_height != self._window.winfo_height():
  42. self._win_height = self._window.winfo_height()
  43. self._win_width = self._window.winfo_width()
  44. self._set_windows()
  45. @abc.abstractmethod
  46. def _create_windows(self):
  47. pass
  48. @abc.abstractmethod
  49. def _set_windows(self):
  50. pass
  51. def mainloop(self):
  52. self._window.mainloop()
  53. class HEnglish(HEnglishTkinter):
  54. def __init__(self):
  55. super(HEnglish, self).__init__("H-English")
  56. def _create_windows(self):
  57. self._title_label = tkinter.Label(self._window)
  58. self._control_frame = tkinter.Frame(self._window)
  59. self._control_btn = [tkinter.Button(self._control_frame) for _ in range(6)]
  60. def _set_windows(self):
  61. self.__conf_title()
  62. self.__conf_control_btn()
  63. def __conf_title(self):
  64. if self._win_width >= self._win_height:
  65. font = tktool.make_font(size=int(self._win_height * 0.06), weight="bold")
  66. else:
  67. font = tktool.make_font(size=int(self._win_width * 0.05), weight="bold")
  68. self._title_label['font'] = font
  69. self._title_label['bg'] = '#F0FFFF'
  70. self._title_label['text'] = "Huan-English-Dictionary" # 使用英语标题在GUI更美观
  71. self._title_label['anchor'] = 'c'
  72. self._title_label.place(relx=0.0, rely=0.03, relwidth=1.0, relheight=0.13)
  73. self._title = tkinter.Label()
  74. def __conf_control_btn(self):
  75. if self._win_width >= self._win_height:
  76. font = tktool.make_font(size=int(self._win_height * 0.03))
  77. self._control_btn[0].place(relx=0.07, rely=0.10, relwidth=0.4, relheight=0.2)
  78. self._control_btn[1].place(relx=0.53, rely=0.10, relwidth=0.4, relheight=0.2)
  79. self._control_btn[2].place(relx=0.07, rely=0.40, relwidth=0.4, relheight=0.2)
  80. self._control_btn[3].place(relx=0.53, rely=0.40, relwidth=0.4, relheight=0.2)
  81. self._control_btn[4].place(relx=0.07, rely=0.70, relwidth=0.4, relheight=0.2)
  82. self._control_btn[5].place(relx=0.53, rely=0.70, relwidth=0.4, relheight=0.2)
  83. else:
  84. font = tktool.make_font(size=int(self._win_width * 0.03))
  85. self._control_btn[0].place(relx=0.1, rely=0.08, relwidth=0.8, relheight=0.1)
  86. self._control_btn[1].place(relx=0.1, rely=0.23, relwidth=0.8, relheight=0.1)
  87. self._control_btn[2].place(relx=0.1, rely=0.38, relwidth=0.8, relheight=0.1)
  88. self._control_btn[3].place(relx=0.1, rely=0.53, relwidth=0.8, relheight=0.1)
  89. self._control_btn[4].place(relx=0.1, rely=0.68, relwidth=0.8, relheight=0.1)
  90. self._control_btn[5].place(relx=0.1, rely=0.83, relwidth=0.8, relheight=0.1)
  91. self._control_frame['bg'] = "#FFFFFF"
  92. self._control_frame['relief'] = "ridge"
  93. self._control_frame['bd'] = 5
  94. self._control_frame.place(relx=0.05, rely=0.20, relwidth=0.90, relheight=0.75)
  95. for i in zip(self._control_btn,
  96. ["Word Test", "Dictionary", "Export", "Import", "Delete", "About"],
  97. ["#1E90FF", "#FF69B4", "#FF4500", "#4169E1", "#00FF7F", "#C71585"],
  98. [None, None, None, self.import_word, None, None]):
  99. i[0]['font'] = font
  100. i[0]['fg'] = "#000000"
  101. i[0]['bg'] = i[2]
  102. i[0]['activeforeground'] = "#FFFFFF"
  103. i[0]['activebackground'] = i[2]
  104. i[0]['anchor'] = 'c'
  105. i[0]['relief'] = "ridge"
  106. i[0]['bd'] = 5
  107. i[0]['text'] = i[1]
  108. i[0]['command'] = i[3]
  109. def import_word(self):
  110. Import(self, self._window)
  111. def show_loading(self, title: str): # 有子线程时显示加载
  112. ...
  113. def stop_loading(self): # 子线程运行完成后关闭加载
  114. ...
  115. class Import(HEnglishTkinter):
  116. def __init__(self, father: HEnglish, father_windows: tkinter.Tk):
  117. super(Import, self).__init__("Import", father, size=(1 / 3, 1 / 3))
  118. self._father = father
  119. self._father_windows = father_windows
  120. def _create_windows(self):
  121. self._title_label = tkinter.Label(self._window)
  122. self._control_btn = [tkinter.Button(self._window) for _ in range(6)]
  123. def _set_windows(self):
  124. self.__conf_title()
  125. self.__conf_control_btn()
  126. def __conf_title(self):
  127. if self._win_width >= self._win_height:
  128. font = tktool.make_font(size=int(self._win_height * 0.06), weight="bold")
  129. else:
  130. font = tktool.make_font(size=int(self._win_width * 0.05), weight="bold")
  131. self._title_label['font'] = font
  132. self._title_label['bg'] = '#F0FFFF'
  133. self._title_label['text'] = "Import Word" # 使用英语标题在GUI更美观
  134. self._title_label['anchor'] = 'c'
  135. self._title_label.place(relx=0.0, rely=0.03, relwidth=1.0, relheight=0.13)
  136. self._title = tkinter.Label()
  137. def __conf_control_btn(self):
  138. if self._win_width >= self._win_height:
  139. font = tktool.make_font(size=int(self._win_height * 0.04))
  140. self._control_btn[0].place(relx=0.07, rely=0.28, relwidth=0.4, relheight=0.2)
  141. self._control_btn[1].place(relx=0.53, rely=0.28, relwidth=0.4, relheight=0.2)
  142. self._control_btn[2].place(relx=0.07, rely=0.66, relwidth=0.4, relheight=0.2)
  143. self._control_btn[3].place(relx=0.53, rely=0.66, relwidth=0.4, relheight=0.2)
  144. else:
  145. font = tktool.make_font(size=int(self._win_width * 0.04))
  146. self._control_btn[0].place(relx=0.1, rely=0.20, relwidth=0.8, relheight=0.1)
  147. self._control_btn[1].place(relx=0.1, rely=0.33, relwidth=0.8, relheight=0.1)
  148. self._control_btn[2].place(relx=0.1, rely=0.46, relwidth=0.8, relheight=0.1)
  149. self._control_btn[3].place(relx=0.1, rely=0.59, relwidth=0.8, relheight=0.1)
  150. for i in zip(self._control_btn,
  151. ["From Text", "From CSV", "From Excel", "From Json"],
  152. ["#1E90FF", "#FF69B4", "#C71585", "#00FF7F"],
  153. [None, None, None, None]):
  154. i[0]['font'] = font
  155. i[0]['fg'] = "#000000"
  156. i[0]['bg'] = i[2]
  157. i[0]['activeforeground'] = "#FFFFFF"
  158. i[0]['activebackground'] = i[2]
  159. i[0]['anchor'] = 'c'
  160. i[0]['relief'] = "ridge"
  161. i[0]['bd'] = 5
  162. i[0]['text'] = i[1]
  163. i[0]['command'] = i[3]
  164. def show_loading(self, title: str): # 有子线程时显示加载
  165. ...
  166. def stop_loading(self): # 子线程运行完成后关闭加载
  167. ...
  168. if __name__ == '__main__':
  169. hgui = HEnglish()
  170. hgui.mainloop()