Skip to content

Commit

Permalink
✨ Refactor with borax.ui.widgets.MessageLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
kinegratii committed Mar 29, 2024
1 parent 248a98a commit 6abc535
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
13 changes: 7 additions & 6 deletions borax/capp/borax_calendar_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from borax.calendars.ui import CalendarFrame, FestivalTableFrame
from borax.calendars.utils import ThreeNineUtils
from borax.capp.festival_creator import FestivalCreatePanel
from borax.ui.widgets import MessageLabel

library = FestivalLibrary.load_builtin().sort_by_countdown()

Expand Down Expand Up @@ -82,7 +83,7 @@ def __init__(self, master=None):

ttk.Button(self._tool_form_frame, text='计算', command=self.run_date_delta).grid(
row=2, column=0, columnspan=4, pady=8)
self.result1_label = ttk.Label(self._tool_form_frame, text='')
self.result1_label = MessageLabel(self._tool_form_frame, text='')
self.result1_label.grid(row=3, column=0, columnspan=4)
notebook.add(self._tool_form_frame, text='日期间隔', padding=4)

Expand All @@ -104,7 +105,7 @@ def __init__(self, master=None):
delta_days_com.grid(row=2, column=2, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S)
ttk.Button(deduction_frame, text='计算', command=self.run_date_deduction).grid(
row=3, column=0, columnspan=4, pady=8)
self.result2_label = ttk.Label(deduction_frame, text='')
self.result2_label = MessageLabel(deduction_frame, text='')
self.result2_label.grid(row=4, column=0, columnspan=4)
# init
self.day_delta_s.set(1)
Expand All @@ -131,17 +132,17 @@ def run_date_delta(self):
d1, d2 = self._data_stores['d1'].get_date(), self._data_stores['d2'].get_date()
if d1 and d2:
ndays = (d2.solar - d1.solar).days
self.result1_label.config(text=f'相差 {ndays} 天')
self.result1_label.show_text(text=f'相差 {ndays} 天')
else:
self.result1_label.config(text='未选择日期,无法计算')
self.result1_label.show_error_splash(text='未选择日期,无法计算')

def run_date_deduction(self):
d3 = self._data_stores['d3'].get_date()
if d3:
result2 = d3 + timedelta(self.day_delta_s.get() * self.delta_days.get())
self.result2_label.config(text=str(result2))
self.result2_label.show_text(str(result2))
else:
self.result2_label.config(text='未选择日期,无法计算')
self.result2_label.show_error_splash(text='未选择日期,无法计算')


class DateDetailFrame(ttk.LabelFrame):
Expand Down
23 changes: 7 additions & 16 deletions borax/capp/festival_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)
from borax.calendars.lunardate import TextUtils, TERMS_CN
from borax.calendars.ui import FestivalTableFrame
from borax.ui.widgets import MessageLabel

__all__ = ['FestivalCreatePanel', 'start_festival_creator']

Expand Down Expand Up @@ -120,16 +121,6 @@ def _validate_f4(self):
return festival


class MessageLabel(ttk.Label):
def splash(self, text: str, timeout: int = 1000, foreground='black', **kwargs):
self.config({'text': text, 'foreground': foreground, **kwargs})
if timeout != 0:
self.after(timeout, self._clear)

def _clear(self):
self.config({'text': '', 'foreground': 'black'})


class FestivalCreatePanel(ttk.Frame):
"""A UI panel with festival-CRUD futures."""

Expand Down Expand Up @@ -269,26 +260,26 @@ def _create(self, event=None):
try:
festival = self._vm.validate()
self._festival_table.add_festival(festival)
self._msg_label.splash(f'{festival.description} {festival.encode()}', foreground='green')
self._msg_label.show_success_splash(f'{festival.description} {festival.encode()}')
except ValidateError as e:
self._msg_label.splash(str(e), foreground='red')
self._msg_label.show_error_splash(str(e))

def _delete(self, event=None):
self._festival_table.delete_selected_festivals()

def _clear_data(self, event=None):
self._festival_table.clear_data()
self._msg_label.splash('清空成功!', foreground='green')
self._msg_label.show_success_splash('清空成功!')

def _export(self, event=None):
if len(self._festival_table.tree_view.get_children()) == 0:
self._msg_label.splash('表格无数据!', foreground='red')
self._msg_label.show_warning_splash('表格无数据!')
return
filename = filedialog.asksaveasfilename(parent=self, title='保存到', defaultextension='.csv',
filetypes=(('csv', 'csv'),))
if filename:
self._festival_table.festival_library.to_csv(filename)
self._msg_label.splash('导出成功', foreground='green')
self._msg_label.show_success_splash('导出成功')

def _on_source_selected(self, val: str):
if val == 'custom':
Expand All @@ -306,7 +297,7 @@ def _open_and_add(self, event=None):

def _load_new_festival_library(self, f_library: FestivalLibrary):
self._festival_table.add_festivals_from_library(f_library)
self._msg_label.splash(f'加载成功,共{self._festival_table.row_count}', foreground='green')
self._msg_label.show_success_splash(f'加载成功,共{self._festival_table.row_count}条')


def start_festival_creator():
Expand Down
9 changes: 9 additions & 0 deletions borax/ui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,14 @@ def show_text(self, text: str, text_color: str = 'black', splash_ms: int = 0):
if splash_ms:
self.after(splash_ms, self._clear)

def show_error_splash(self, text: str, splash_ms: int = 1000):
self.show_text(text, text_color='error', splash_ms=splash_ms)

def show_warning_splash(self, text: str, splash_ms: int = 1000):
self.show_text(text, text_color='warning', splash_ms=splash_ms)

def show_success_splash(self, text: str, splash_ms: int = 1000):
self.show_text(text, text_color='success', splash_ms=splash_ms)

def _clear(self):
self.config({'text': ''})

0 comments on commit 6abc535

Please sign in to comment.