Преглед изворни кода

feat: 添加tkinter实用工具

SongZihuan пре 3 година
родитељ
комит
c15c52f7b4
4 измењених фајлова са 57 додато и 68 уклоњено
  1. 15 24
      control/admin.py
  2. 4 8
      control/ranking.py
  3. 24 36
      control/station.py
  4. 14 0
      tool/tk.py

+ 15 - 24
control/admin.py

@@ -1,12 +1,12 @@
 import tkinter as tk
 from tkinter import ttk
 from tkinter import messagebox as msg
-import tkinter.font as font
 import abc
 import time
 
 import conf
 from tool.type_ import *
+from tool.tk import set_button_disable_from_list, make_font
 
 from sql.db import DB
 from sql.user import creat_new_user, del_user, find_user_by_name
@@ -218,7 +218,7 @@ class AdminStation(AdminStationBase):
         self.to_program()
 
     def __conf_win_ctrl_button(self):
-        title_font = self.make_font(size=self._win_ctrl_font_size)
+        title_font = make_font(size=self._win_ctrl_font_size)
 
         for bt in self._win_ctrl_button:
             bt: tk.Button
@@ -271,7 +271,7 @@ class AdminStation(AdminStationBase):
         self._menu_back['bd'] = 5
         self._menu_back['relief'] = "ridge"
 
-        title_font = self.make_font(size=self._menu_title_font_size, weight="bold")
+        title_font = make_font(size=self._menu_title_font_size, weight="bold")
         self._menu_title[0]['bg'] = '#fffffb'
         self._menu_title[0]['font'] = title_font
         self._menu_title[0]['textvariable'] = self._menu_title[1]
@@ -298,7 +298,7 @@ class AdminStation(AdminStationBase):
         self._program_back['relief'] = "ridge"
         self._program_back['bd'] = 5
 
-        title_font = self.make_font(size=self._program_title_font_size, weight="bold")
+        title_font = make_font(size=self._program_title_font_size, weight="bold")
         self._program_title[0]['bg'] = '#2468a2'
         self._program_title[0]['fg'] = "#F0F8FF"
         self._program_title[0]['font'] = title_font
@@ -361,8 +361,8 @@ class AdminStation(AdminStationBase):
         self.set_reset_all_btn()
 
     def __conf_msg(self):
-        title_font = self.make_font(size=self._msg_font_size + 1, weight="bold")
-        info_font = self.make_font(size=self._msg_font_size - 1)
+        title_font = make_font(size=self._msg_font_size + 1, weight="bold")
+        info_font = make_font(size=self._msg_font_size - 1)
 
         self._msg_frame['bg'] = "#fffffb"
         self._msg_frame['bd'] = 5
@@ -454,8 +454,8 @@ class AdminStation(AdminStationBase):
         self.hide_main()
 
     def __conf_login_window(self):
-        title_font = self.make_font(size=self._login_title_font_size, weight="bold")
-        btn_font = self.make_font(size=self._login_btn_font_size, weight="bold")
+        title_font = make_font(size=self._login_title_font_size, weight="bold")
+        btn_font = make_font(size=self._login_btn_font_size, weight="bold")
 
         for lb, text in zip([self._login_name[0], self._login_passwd[0]], ["User:", "Passwd:"]):
             lb['bg'] = "#d1d9e0"  # 蜜瓜绿
@@ -519,15 +519,6 @@ class AdminStation(AdminStationBase):
         self._window.update()
         self._window.deiconify()
 
-    @staticmethod
-    def make_font(family: str = 'noto', **kwargs):
-        return font.Font(family=conf.font_d[family], **kwargs)
-
-    @staticmethod
-    def set_button_disable_from_list(btn_list: List[tk.Button], flat: str = 'disable'):
-        for btn in btn_list:
-            btn['state'] = flat
-
     def mainloop(self):
         self._window.mainloop()
 
@@ -631,7 +622,7 @@ class MainMenu(AdminMenu):
     def conf_gui(self, color: str, n: int = 1):
         self.__conf_font(n)
 
-        btn_font = self.station.make_font(size=self.btn_font_size, weight="bold")
+        btn_font = make_font(size=self.btn_font_size, weight="bold")
         height = 0.02
         for btn, text in zip(self.btn, ["Creat", "Delete", "Search", "Update", "Logout"]):
             btn['font'] = btn_font
@@ -644,10 +635,10 @@ class MainMenu(AdminMenu):
         return "Main", self.frame
 
     def set_disable(self):
-        self.station.set_button_disable_from_list(self.btn)
+        set_button_disable_from_list(self.btn)
 
     def reset_disable(self):
-        self.station.set_button_disable_from_list(self.btn, flat='normal')
+        set_button_disable_from_list(self.btn, flat='normal')
 
 
 class WelcomeProgram(AdminProgram):
@@ -667,8 +658,8 @@ class WelcomeProgram(AdminProgram):
     def conf_gui(self, n: int = 1):
         self.__conf_font(n)
 
-        title_font = self.station.make_font(size=self.title_font_size, weight="bold")
-        btn_font = self.station.make_font(size=self.btn_font_size)
+        title_font = make_font(size=self.title_font_size, weight="bold")
+        btn_font = make_font(size=self.btn_font_size)
 
         for btn, text in zip(self.btn, ["TestMSG", "TestProgress"]):
             btn['font'] = btn_font
@@ -697,10 +688,10 @@ class WelcomeProgram(AdminProgram):
         return "Welcome", self.frame
 
     def set_disable(self):
-        self.station.set_button_disable_from_list(self.btn)
+        set_button_disable_from_list(self.btn)
 
     def reset_disable(self):
-        self.station.set_button_disable_from_list(self.btn, flat='normal')
+        set_button_disable_from_list(self.btn, flat='normal')
 
 
 if __name__ == '__main__':

+ 4 - 8
control/ranking.py

@@ -1,10 +1,10 @@
 import abc
 import tkinter as tk
-import tkinter.font as font
 from PIL import Image, ImageTk
 
 import conf
 from tool.type_ import *
+from tool.tk import make_font
 from sql.db import DB
 
 
@@ -255,9 +255,9 @@ class RankingStation(RankingStationBase):
         self.show_rank_first()
 
     def __conf_rank(self):
-        title_font = self.__make_font(size=self._rank_font_title_size, weight="bold")
-        info_font = self.__make_font(size=self._rank_font_size)
-        btn_font = self.__make_font(size=self._rank_font_btn_size)
+        title_font = make_font(size=self._rank_font_title_size, weight="bold")
+        info_font = make_font(size=self._rank_font_size)
+        btn_font = make_font(size=self._rank_font_btn_size)
 
         height = self.height * 0.95
         width = height * (3 / 4)
@@ -343,10 +343,6 @@ class RankingStation(RankingStationBase):
             relheight = self.rank_y_height[i][1]
             self.rank_label[i].place(relx=0.04, rely=rely, relwidth=0.92, relheight=relheight)
 
-    @staticmethod
-    def __make_font(family: str = 'noto', **kwargs):
-        return font.Font(family=conf.font_d[family], **kwargs)
-
     def show_rank(self, rank_info: List[Tuple[int, uname_t, uid_t, score_t, score_t, Optional[str]]]):
         self.__set_rank_info(rank_info)
         self.rank_title_var.set("Ranking")

+ 24 - 36
control/station.py

@@ -7,11 +7,11 @@ import traceback
 import abc
 import tkinter as tk
 from tkinter import ttk
-import tkinter.font as font
 import datetime
 from PIL import Image, ImageTk
 
 from tool.type_ import *
+from tool.tk import set_button_disable_from_list, make_font
 
 from core.user import User, UserNotSupportError
 from core.garbage import GarbageBag, GarbageType, GarbageBagNotUse
@@ -636,7 +636,7 @@ class GarbageStation(GarbageStationBase):
         self._window.overrideredirect(show)
 
     def __conf_title_label(self):
-        title_font = self.__make_font(size=self._title_font_size, weight="bold")
+        title_font = make_font(size=self._title_font_size, weight="bold")
         self._title_label['font'] = title_font
         self._title_label['bg'] = "#F0FFF0"  # 蜜瓜绿
         self._title_label['text'] = "HGSSystem: GarbageStation Control Center"
@@ -644,7 +644,7 @@ class GarbageStation(GarbageStationBase):
         self._title_label.place(relx=0.02, rely=0.0, relwidth=0.6, relheight=0.07)
 
     def __conf_win_ctrl_button(self):
-        title_font = self.__make_font(size=self._win_ctrl_font_size)
+        title_font = make_font(size=self._win_ctrl_font_size)
 
         for bt in self._win_ctrl_button:
             bt: tk.Button
@@ -670,8 +670,8 @@ class GarbageStation(GarbageStationBase):
         bt_exit.place(relx=0.93, rely=0.01, relwidth=0.05, relheight=0.05)
 
     def __conf_user_info_label(self):
-        title_font = self.__make_font(size=self._win_info_font_size - 1, weight="bold")
-        info_font = self.__make_font(size=self._win_info_font_size + 1)
+        title_font = make_font(size=self._win_info_font_size - 1, weight="bold")
+        info_font = make_font(size=self._win_info_font_size + 1)
 
         frame_width = self._win_width * 0.4
         frame_height = self._win_height * 0.4
@@ -725,7 +725,7 @@ class GarbageStation(GarbageStationBase):
         self._user_img['relief'] = "ridge"
 
     def __conf_throw_btn(self):
-        btn_font = self.__make_font(size=self._throw_ctrl_btn_font_size, weight="bold")
+        btn_font = make_font(size=self._throw_ctrl_btn_font_size, weight="bold")
         btn_info: List[Tuple[str, str]] = [("Recyclable", "#00BFFF"),
                                            ("Other", "#A9A9A9"),
                                            ("Hazardous", "#DC143C"),
@@ -749,7 +749,7 @@ class GarbageStation(GarbageStationBase):
         self._throw_ctrl_btn[3].place(relx=0.505, rely=0.505, relwidth=0.495, relheight=0.495)
 
     def __conf_check_btn(self):
-        btn_font = self.__make_font(size=self._check_ctrl_btn_font_size, weight="bold")
+        btn_font = make_font(size=self._check_ctrl_btn_font_size, weight="bold")
         btn_info: List[Tuple[str, str]] = [("Fail", "#ef7a82"),
                                            ("Pass", "#70f3ff")]
 
@@ -767,8 +767,8 @@ class GarbageStation(GarbageStationBase):
         self._check_ctrl_btn[1].place(relx=0.505, rely=0.000, relwidth=0.495, relheight=1)
 
     def __conf_sys_info_label(self):
-        title_font = self.__make_font(size=self._sys_info_font_size - 1, weight="bold")
-        info_font = self.__make_font(size=self._sys_info_font_size + 1)
+        title_font = make_font(size=self._sys_info_font_size - 1, weight="bold")
+        info_font = make_font(size=self._sys_info_font_size + 1)
 
         self._sys_info_frame['bg'] = "#F0F8FF"
         self._sys_info_frame.place(relx=0.02, rely=0.51, relwidth=0.4, relheight=0.14)
@@ -801,7 +801,7 @@ class GarbageStation(GarbageStationBase):
             height += height_label + h_label_s / height_count
 
     def __conf_user_btn(self):
-        btn_font = self.__make_font(size=self._user_btn_font_size, weight="bold")
+        btn_font = make_font(size=self._user_btn_font_size, weight="bold")
         btn_info: List[Tuple[str, str]] = [("Detail", "#b0a4e3"),
                                            ("Ranking", "#b0a4e3"),
                                            ("Search", "#b0a4e3")]
@@ -831,8 +831,8 @@ class GarbageStation(GarbageStationBase):
         self._cap_label.place(relx=0.22, rely=0.66, relwidth=0.2, relheight=0.32)
 
     def __conf_msg(self):
-        title_font = self.__make_font(size=self._msg_font_size + 1, weight="bold")
-        info_font = self.__make_font(size=self._msg_font_size - 1)
+        title_font = make_font(size=self._msg_font_size + 1, weight="bold")
+        info_font = make_font(size=self._msg_font_size - 1)
 
         self._msg_frame['bg'] = "#F5FFFA"
         self._msg_frame['bd'] = 5
@@ -887,9 +887,9 @@ class GarbageStation(GarbageStationBase):
         self.show_msg(title, info, msg_type='Warning')
 
     def __conf_rank(self):
-        title_font = self.__make_font(size=self._rank_font_title_size, weight="bold")
-        info_font = self.__make_font(size=self._rank_font_size)
-        btn_font = self.__make_font(size=self._msg_font_size - 1)
+        title_font = make_font(size=self._rank_font_title_size, weight="bold")
+        info_font = make_font(size=self._rank_font_size)
+        btn_font = make_font(size=self._msg_font_size - 1)
 
         self._rank_frame['bg'] = "#F5F5DC"
         self._rank_frame['relief'] = "ridge"
@@ -981,7 +981,7 @@ class GarbageStation(GarbageStationBase):
             self.update_user_time()
 
     def __conf_loading(self):
-        title_font = self.__make_font(size=self._loading_tile_font, weight="bold")
+        title_font = make_font(size=self._loading_tile_font, weight="bold")
 
         self._loading_frame['bg'] = "#808080"
         self._loading_frame['bd'] = 5
@@ -1127,46 +1127,34 @@ class GarbageStation(GarbageStationBase):
         self._user_btn[0]['state'] = 'disable'
 
     def normal_user_disable(self):
-        for btn in self._check_ctrl_btn:
-            btn['state'] = 'disabled'
+        set_button_disable_from_list(self._check_ctrl_btn, flat='disable')
         self._user_btn[0]['state'] = 'normal'
         self._user_btn[0]['command'] = lambda: self.show_user_info()
 
     def manager_user_disable(self):
-        for btn in self._throw_ctrl_btn:
-            btn['state'] = 'disabled'
+        set_button_disable_from_list(self._throw_ctrl_btn, flat='disable')
         self._user_btn[0]['state'] = 'normal'
         self._user_btn[0]['command'] = lambda: self.show_garbage_info()
 
     def normal_user_able(self):
-        for btn in self._throw_ctrl_btn:
-            btn['state'] = 'normal'
+        set_button_disable_from_list(self._throw_ctrl_btn, flat='normal')
 
     def manager_user_able(self):
-        for btn in self._check_ctrl_btn:
-            btn['state'] = 'normal'
+        set_button_disable_from_list(self._check_ctrl_btn, flat='normal')
 
     def set_all_btn_disable(self):
         self.__switch_to_no_user()  # 禁用所有操作性按钮
         self.hide_msg_rank()
-        for btn in self._user_btn:
-            btn['state'] = 'disable'
-        for btn in self._win_ctrl_button:
-            btn['state'] = 'disable'
+        set_button_disable_from_list(self._user_btn, flat='disable')
+        set_button_disable_from_list(self._win_ctrl_button, flat='disable')
         self._disable_all_btn = True
 
     def set_reset_all_btn(self):
-        for btn in self._user_btn:
-            btn['state'] = 'normal'
-        for btn in self._win_ctrl_button:
-            btn['state'] = 'normal'
+        set_button_disable_from_list(self._user_btn, flat='normal')
+        set_button_disable_from_list(self._win_ctrl_button, flat='normal')
         self.update_control()  # 位于_user_btn之后, 会自动设定detail按钮
         self._disable_all_btn = False
 
-    @staticmethod
-    def __make_font(family: str = 'noto', **kwargs):
-        return font.Font(family=conf.font_d[family], **kwargs)
-
     def mainloop(self):
         self._window.mainloop()
 

+ 14 - 0
tool/tk.py

@@ -0,0 +1,14 @@
+import tkinter as tk
+from tkinter import font
+from typing import List
+
+import conf
+
+
+def make_font(family: str = 'noto', **kwargs):
+    return font.Font(family=conf.font_d[family], **kwargs)
+
+
+def set_button_disable_from_list(btn_list: List[tk.Button], flat: str = 'disable'):
+    for btn in btn_list:
+        btn['state'] = flat