Fix calendar on Linux and other stuff

This commit is contained in:
Ircama 2024-08-09 02:41:19 +02:00
parent 263398f865
commit 178369e9f5

95
ui.py
View file

@ -1,3 +1,4 @@
import sys
import re import re
import threading import threading
import ipaddress import ipaddress
@ -124,6 +125,19 @@ class ToolTip:
return "\n".join(lines) return "\n".join(lines)
class BugFixedDateEntry(DateEntry):
"""
Fixes a bug on the calendar that does not accept mouse selection with Linux
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def drop_down(self):
super().drop_down()
if self._top_cal is not None and not self._calendar.winfo_ismapped():
self._top_cal.lift()
class EpsonPrinterUI(tk.Tk): class EpsonPrinterUI(tk.Tk):
def __init__(self, conf_dict={}, replace_conf=False): def __init__(self, conf_dict={}, replace_conf=False):
super().__init__() super().__init__()
@ -287,13 +301,13 @@ class EpsonPrinterUI(tk.Tk):
ti_received_frame.columnconfigure(2, weight=0) # Button column on the right ti_received_frame.columnconfigure(2, weight=0) # Button column on the right
# TI Received Time Calendar Widget # TI Received Time Calendar Widget
self.date_entry = DateEntry( self.date_entry = BugFixedDateEntry(
ti_received_frame, date_pattern="yyyy-mm-dd" ti_received_frame, date_pattern="yyyy-mm-dd"
) )
self.date_entry.grid( self.date_entry.grid(
row=0, column=1, padx=PADX, pady=PADY, sticky=(tk.W, tk.E) row=0, column=1, padx=PADX, pady=PADY, sticky=(tk.W, tk.E)
) )
self.date_entry.delete(0, "end") self.date_entry.delete(0, "end") # blank the field removing the current date
ToolTip(self.date_entry, "Enter a valid date with format YYYY-MM-DD.") ToolTip(self.date_entry, "Enter a valid date with format YYYY-MM-DD.")
# TI Received Time Buttons # TI Received Time Buttons
@ -467,6 +481,14 @@ class EpsonPrinterUI(tk.Tk):
model=model, model=model,
hostname=ip_address hostname=ip_address
) )
if not printer.parm.get("stats", {}).get("Power off timer"):
self.status_text.insert(
tk.END,
f"[ERROR]: Missing 'Power off timer' in configuration\n",
)
self.config(cursor="")
self.update_idletasks()
return
try: try:
po_timer = printer.stats()["stats"]["Power off timer"] po_timer = printer.stats()["stats"]["Power off timer"]
self.status_text.insert( self.status_text.insert(
@ -474,10 +496,7 @@ class EpsonPrinterUI(tk.Tk):
) )
self.po_timer_var.set(po_timer) self.po_timer_var.set(po_timer)
except Exception as e: except Exception as e:
self.status_text.insert( self.status_text.insert(tk.END, f"[ERROR] {e}\n")
tk.END,
f"[ERROR] {e}: Missing 'Power off timer' in configuration\n",
)
finally: finally:
self.config(cursor="") self.config(cursor="")
self.update_idletasks() self.update_idletasks()
@ -504,8 +523,14 @@ class EpsonPrinterUI(tk.Tk):
model=model, model=model,
hostname=ip_address hostname=ip_address
) )
try: if not printer.parm.get("stats", {}).get("Power off timer"):
po_timer = printer.stats()["stats"]["Power off timer"] self.status_text.insert(
tk.END,
f"[ERROR]: Missing 'Power off timer' in configuration\n",
)
self.config(cursor="")
self.update_idletasks()
return
po_timer = self.po_timer_var.get() po_timer = self.po_timer_var.get()
self.config(cursor="") self.config(cursor="")
self.update_idletasks() self.update_idletasks()
@ -521,18 +546,16 @@ class EpsonPrinterUI(tk.Tk):
"Confirm Action", "Are you sure you want to proceed?" "Confirm Action", "Are you sure you want to proceed?"
) )
if response: if response:
try:
printer.write_poweroff_timer(int(po_timer)) printer.write_poweroff_timer(int(po_timer))
except Exception as e:
self.status_text.insert(tk.END, f"[ERROR] {e}\n")
else: else:
self.status_text.insert( self.status_text.insert(
tk.END, f"[WARNING] Set Power off timer aborted.\n" tk.END, f"[WARNING] Set Power off timer aborted.\n"
) )
except Exception as e:
self.config(cursor="") self.config(cursor="")
self.update_idletasks() self.update_idletasks()
self.status_text.insert(
tk.END,
f"[ERROR] {e}: Cannot set 'Power off timer'; missing configuration\n",
)
def get_ti_date(self, cursor=True): def get_ti_date(self, cursor=True):
if cursor: if cursor:
@ -556,6 +579,14 @@ class EpsonPrinterUI(tk.Tk):
model=model, model=model,
hostname=ip_address hostname=ip_address
) )
if not printer.parm.get("stats", {}).get("First TI received time"):
self.status_text.insert(
tk.END,
f"[ERROR]: Missing 'First TI received time' in configuration\n",
)
self.config(cursor="")
self.update_idletasks()
return
try: try:
date_string = datetime.strptime( date_string = datetime.strptime(
printer.stats()["stats"]["First TI received time"], "%d %b %Y" printer.stats()["stats"]["First TI received time"], "%d %b %Y"
@ -566,10 +597,7 @@ class EpsonPrinterUI(tk.Tk):
) )
self.date_entry.set_date(date_string) self.date_entry.set_date(date_string)
except Exception as e: except Exception as e:
self.status_text.insert( self.status_text.insert(tk.END, f"[ERROR] {e}\n")
tk.END,
f"[ERROR] {e}: Missing 'First TI received time' in configuration\n",
)
finally: finally:
self.config(cursor="") self.config(cursor="")
self.update_idletasks() self.update_idletasks()
@ -596,10 +624,14 @@ class EpsonPrinterUI(tk.Tk):
model=model, model=model,
hostname=ip_address hostname=ip_address
) )
try: if not printer.parm.get("stats", {}).get("First TI received time"):
date_string = datetime.strptime( self.status_text.insert(
printer.stats()["stats"]["First TI received time"], "%d %b %Y" tk.END,
).strftime("%y-%m-%d") f"[ERROR]: Missing 'First TI received time' in configuration\n",
)
self.config(cursor="")
self.update_idletasks()
return
date_string = self.date_entry.get_date() date_string = self.date_entry.get_date()
self.status_text.insert( self.status_text.insert(
tk.END, tk.END,
@ -609,20 +641,17 @@ class EpsonPrinterUI(tk.Tk):
"Confirm Action", "Are you sure you want to proceed?" "Confirm Action", "Are you sure you want to proceed?"
) )
if response: if response:
try:
printer.write_first_ti_received_time( printer.write_first_ti_received_time(
date_string.year, date_string.month, date_string.day date_string.year, date_string.month, date_string.day
) )
except Exception as e:
self.status_text.insert(tk.END, f"[ERROR] {e}\n")
else: else:
self.status_text.insert( self.status_text.insert(
tk.END, tk.END,
f"[WARNING] Change of 'First TI received time' aborted.\n", f"[WARNING] Change of 'First TI received time' aborted.\n",
) )
except Exception as e:
self.status_text.insert(
tk.END,
f"[ERROR] {e}: Cannot set 'First TI received time'; missing configuration\n",
)
finally:
self.config(cursor="") self.config(cursor="")
self.update_idletasks() self.update_idletasks()
@ -711,23 +740,21 @@ class EpsonPrinterUI(tk.Tk):
model=model, model=model,
hostname=ip_address hostname=ip_address
) )
try:
printer.stats() # query the printer first
response = messagebox.askyesno( response = messagebox.askyesno(
"Confirm Action", "Are you sure you want to proceed?" "Confirm Action", "Are you sure you want to proceed?"
) )
if response: if response:
try:
printer.reset_waste_ink_levels() printer.reset_waste_ink_levels()
self.status_text.insert( self.status_text.insert(
tk.END, "[INFO] Waste ink levels have been reset.\n" tk.END, "[INFO] Waste ink levels have been reset.\n"
) )
except Exception as e:
self.status_text.insert(tk.END, f"[ERROR] {e}\n")
else: else:
self.status_text.insert( self.status_text.insert(
tk.END, f"[WARNING] Waste ink levels reset aborted.\n" tk.END, f"[WARNING] Waste ink levels reset aborted.\n"
) )
except Exception as e:
self.status_text.insert(tk.END, f"[ERROR] {e}\n")
finally:
self.config(cursor="") self.config(cursor="")
self.update_idletasks() self.update_idletasks()
@ -916,4 +943,8 @@ if __name__ == "__main__":
conf_dict = pickle.load(args.pickle[0]) conf_dict = pickle.load(args.pickle[0])
app = EpsonPrinterUI(conf_dict=conf_dict, replace_conf=args.override) app = EpsonPrinterUI(conf_dict=conf_dict, replace_conf=args.override)
try:
app.mainloop() app.mainloop()
except:
print("\nInterrupted.")
sys.exit(0)