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 threading
import ipaddress
@ -124,6 +125,19 @@ class ToolTip:
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):
def __init__(self, conf_dict={}, replace_conf=False):
super().__init__()
@ -287,13 +301,13 @@ class EpsonPrinterUI(tk.Tk):
ti_received_frame.columnconfigure(2, weight=0) # Button column on the right
# TI Received Time Calendar Widget
self.date_entry = DateEntry(
self.date_entry = BugFixedDateEntry(
ti_received_frame, date_pattern="yyyy-mm-dd"
)
self.date_entry.grid(
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.")
# TI Received Time Buttons
@ -467,6 +481,14 @@ class EpsonPrinterUI(tk.Tk):
model=model,
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:
po_timer = printer.stats()["stats"]["Power off timer"]
self.status_text.insert(
@ -474,10 +496,7 @@ class EpsonPrinterUI(tk.Tk):
)
self.po_timer_var.set(po_timer)
except Exception as e:
self.status_text.insert(
tk.END,
f"[ERROR] {e}: Missing 'Power off timer' in configuration\n",
)
self.status_text.insert(tk.END, f"[ERROR] {e}\n")
finally:
self.config(cursor="")
self.update_idletasks()
@ -504,8 +523,14 @@ class EpsonPrinterUI(tk.Tk):
model=model,
hostname=ip_address
)
try:
po_timer = printer.stats()["stats"]["Power off timer"]
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
po_timer = self.po_timer_var.get()
self.config(cursor="")
self.update_idletasks()
@ -521,18 +546,16 @@ class EpsonPrinterUI(tk.Tk):
"Confirm Action", "Are you sure you want to proceed?"
)
if response:
try:
printer.write_poweroff_timer(int(po_timer))
except Exception as e:
self.status_text.insert(tk.END, f"[ERROR] {e}\n")
else:
self.status_text.insert(
tk.END, f"[WARNING] Set Power off timer aborted.\n"
)
except Exception as e:
self.config(cursor="")
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):
if cursor:
@ -556,6 +579,14 @@ class EpsonPrinterUI(tk.Tk):
model=model,
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:
date_string = datetime.strptime(
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)
except Exception as e:
self.status_text.insert(
tk.END,
f"[ERROR] {e}: Missing 'First TI received time' in configuration\n",
)
self.status_text.insert(tk.END, f"[ERROR] {e}\n")
finally:
self.config(cursor="")
self.update_idletasks()
@ -596,10 +624,14 @@ class EpsonPrinterUI(tk.Tk):
model=model,
hostname=ip_address
)
try:
date_string = datetime.strptime(
printer.stats()["stats"]["First TI received time"], "%d %b %Y"
).strftime("%y-%m-%d")
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
date_string = self.date_entry.get_date()
self.status_text.insert(
tk.END,
@ -609,20 +641,17 @@ class EpsonPrinterUI(tk.Tk):
"Confirm Action", "Are you sure you want to proceed?"
)
if response:
try:
printer.write_first_ti_received_time(
date_string.year, date_string.month, date_string.day
)
except Exception as e:
self.status_text.insert(tk.END, f"[ERROR] {e}\n")
else:
self.status_text.insert(
tk.END,
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.update_idletasks()
@ -711,23 +740,21 @@ class EpsonPrinterUI(tk.Tk):
model=model,
hostname=ip_address
)
try:
printer.stats() # query the printer first
response = messagebox.askyesno(
"Confirm Action", "Are you sure you want to proceed?"
)
if response:
try:
printer.reset_waste_ink_levels()
self.status_text.insert(
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:
self.status_text.insert(
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.update_idletasks()
@ -916,4 +943,8 @@ if __name__ == "__main__":
conf_dict = pickle.load(args.pickle[0])
app = EpsonPrinterUI(conf_dict=conf_dict, replace_conf=args.override)
try:
app.mainloop()
except:
print("\nInterrupted.")
sys.exit(0)