From b6ba25e1622da28950acbf476df9219498ea3b7a Mon Sep 17 00:00:00 2001 From: kinegratii Date: Thu, 18 Jan 2024 21:37:30 +0800 Subject: [PATCH] :sparkles: Add change festival source --- borax/calendars/ui.py | 9 +++++++-- borax/capp/borax_calendar_app.py | 20 ++++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/borax/calendars/ui.py b/borax/calendars/ui.py index 2a06d70..b329f4f 100644 --- a/borax/calendars/ui.py +++ b/borax/calendars/ui.py @@ -183,7 +183,7 @@ class FestivalTableFrame(ttk.Frame): """A table frame displaying festivals with CURD feature.""" def __init__(self, master=None, columns: Sequence = None, festival_source: Union[str, FestivalLibrary] = 'empty', - **kwargs): + countdown_ordered: bool = False, **kwargs): super().__init__(master=master, **kwargs) self._adapter = FestivalItemAdapter(columns) if isinstance(festival_source, FestivalLibrary): @@ -198,6 +198,7 @@ def __init__(self, master=None, columns: Sequence = None, festival_source: Union for i, name in enumerate(self._adapter.displays, start=1): self._tree.column(f"# {i}", anchor=tk.CENTER, width=self._adapter.widths[i - 1]) self._tree.heading(f"# {i}", text=name) + self._countdown_ordered = countdown_ordered self.notify_data_changed() @@ -213,11 +214,15 @@ def festival_library(self) -> FestivalLibrary: def row_count(self): return len(self._tree.get_children()) + def change_festival_source(self, source: str): + self._library = FestivalLibrary.load_builtin(source) + self.notify_data_changed() + def notify_data_changed(self): item_iids = self._tree.get_children() if len(item_iids): self._tree.delete(*item_iids) - for ndays, wd, festival in self._library.list_days_in_countdown(countdown_ordered=False): + for ndays, wd, festival in self._library.list_days_in_countdown(countdown_ordered=self._countdown_ordered): values = self._adapter.object2values(festival, wd, ndays) self._tree.insert('', 'end', text="1", values=values) diff --git a/borax/capp/borax_calendar_app.py b/borax/capp/borax_calendar_app.py index 1ecb020..3e9b8b6 100644 --- a/borax/capp/borax_calendar_app.py +++ b/borax/capp/borax_calendar_app.py @@ -186,7 +186,8 @@ def __init__(self, master=None, **kwargs): gz_grid_frame = ttk.Frame(self) for offset in range(60): row, col = offset // 10, offset % 10 - btn = tk.Button(gz_grid_frame, text=TextUtils.get_gz_cn(offset), width=3, height=1, + btn_text = '{} {}'.format(TextUtils.offset2gz(offset), offset + 1) + btn = tk.Button(gz_grid_frame, text=btn_text, width=5, height=1, command=lambda go=offset: self._show_years(go), relief=tk.GROOVE) btn.grid(row=row, column=col, ipadx=5, ipady=5) gz_grid_frame.pack(side='left') @@ -237,12 +238,14 @@ def __init__(self, master=None): self.cal_panel.bind_date_selected(self.on_show_date_detail) ttk.Separator(self, orient=tk.VERTICAL).pack(side='left', fill=tk.Y, expand=True) + self._table_festival_libray = library columns = (("name", 100), ("description", 180), ("code", 80), ("next_day", 150), ("countdown", 60)) - cs = FestivalTableFrame(self, columns=columns, festival_source=library) - cs.pack(side='right', expand=True, fill=tk.BOTH, padx=10, pady=10) + self._cs = FestivalTableFrame(self, columns=columns, festival_source=library, countdown_ordered=True) + self._cs.pack(side='right', expand=True, fill=tk.BOTH, padx=10, pady=10) # cs.update_data() self._style_var = tk.StringVar() + self._table_festival_source_var = tk.StringVar(value='basic') self.create_top_menu() self._tool_dlg = None self._gz_dlg = None @@ -262,9 +265,11 @@ def create_top_menu(self): menu_bar.add_command(label='日期计算', command=self.start_tool_dlg) menu_bar.add_command(label='干支节气', command=self.start_gz_dlg) menu_bar.add_command(label='创建节日', command=self.start_festival_dlg) - settings_menu = tk.Menu(menu_bar) - settings_menu.add_command(label='节日源') - menu_bar.add_cascade(label='设置', menu=settings_menu) + source_menu = tk.Menu(menu_bar) + for source in ('basic', 'ext1'): + source_menu.add_radiobutton(label=source, variable=self._table_festival_source_var, + command=self._change_source) + menu_bar.add_cascade(label='节日源', menu=source_menu) about_menu = tk.Menu(menu_bar) about_menu.add_command(label='项目主页', command=lambda: webbrowser.open(PROJECT_URLS['home'])) about_menu.add_command(label='关于软件', command=self.show_about_info) @@ -274,6 +279,9 @@ def _change_theme(self): global style style.theme_use(self._style_var.get()) + def _change_source(self): + self._cs.change_festival_source(self._table_festival_source_var.get()) + def on_show_date_detail(self, wd: WrappedDate): self.detail_frame.set_selected_date(wd)