from datetime import datetime import tkinter as tk from . import globals as g class FrmHistory(tk.Toplevel): FONT = ("Consolas", 12, "bold") def __init__(self, master): super().__init__(master) self.title("History") self.my_gun = None self._build_ui() self.set_gun_height(549) def _build_ui(self): self.lbl_title = tk.Label(self, text="", anchor="center", font=self.FONT) self.lbl_title.place(x=13, y=9, width=395, height=23) self._lbl_fired = tk.Label(self, text="Fired: ", anchor="e", font=self.FONT) self._lbl_fired.place(x=13, y=32, width=134, height=23) self.lbl_fired = tk.Label(self, text="", anchor="w", font=self.FONT) self.lbl_fired.place(x=155, y=32, width=253, height=23) self._lbl_time = tk.Label(self, text="On Time: ", anchor="e", font=self.FONT) self._lbl_time.place(x=13, y=55, width=134, height=23) self.lbl_time = tk.Label(self, text="", anchor="w", font=self.FONT) self.lbl_time.place(x=155, y=55, width=253, height=23) self._lbl_hist = tk.Label(self, text="HISTORICAL", anchor="center", bg="black", fg="gray", font=self.FONT) self._lbl_hist.place(x=1, y=103, width=419, height=23) self._lbl_fired_hist = tk.Label(self, text="Fired: ", anchor="e", font=self.FONT) self._lbl_fired_hist.place(x=13, y=126, width=134, height=23) self.lbl_fired_hist = tk.Label(self, text="", anchor="w", font=self.FONT) self.lbl_fired_hist.place(x=155, y=126, width=253, height=23) self._lbl_time_hist = tk.Label(self, text="On Time: ", anchor="e", font=self.FONT) self._lbl_time_hist.place(x=13, y=149, width=134, height=23) self.lbl_time_hist = tk.Label(self, text="", anchor="w", font=self.FONT) self.lbl_time_hist.place(x=155, y=149, width=253, height=23) self.btn_cancel = tk.Button(self, text="Cancel", font=self.FONT, command=self.destroy) self.btn_cancel.place(x=333, y=58, width=75, height=42) self.btn_change_batteries = tk.Button(self, text="Change Batteries", font=self.FONT, command=self._btn_change_batteries) self.btn_change_batteries.place(x=12, y=198, width=396, height=36) self.txt_history = tk.Text(self, width=50, height=13, state="disabled", font=self.FONT) self.txt_history.place(x=12, y=240, width=397, height=258) self.txt_set_fired = tk.Entry(self, font=self.FONT) self.txt_set_fired.place(x=12, y=504, width=100, height=26) self.btn_set_fired = tk.Button(self, text="SetFired", font=self.FONT, command=self._btn_set_fired) self.btn_set_fired.place(x=118, y=504, width=113, height=26) self.txt_set_time = tk.Entry(self, font=self.FONT) self.txt_set_time.place(x=12, y=536, width=100, height=26) self.btn_set_time = tk.Button(self, text="SetTime", font=self.FONT, command=self._btn_set_time) self.btn_set_time.place(x=118, y=536, width=113, height=26) def set_gun_height(self, height): self.geometry("421x{}".format(height)) def set_gun(self, gun): self.my_gun = gun admin_widgets = (self.txt_set_fired, self.txt_set_time, self.btn_set_fired, self.btn_set_time) positions = { self.txt_set_fired: (12, 504, 100, 26), self.btn_set_fired: (118, 504, 113, 26), self.txt_set_time: (12, 536, 100, 26), self.btn_set_time: (118, 536, 113, 26), } if g.admin_mode: self.set_gun_height(605) for w in admin_widgets: x, y, ww, h = positions[w] w.place(x=x, y=y, width=ww, height=h) else: self.set_gun_height(549) for w in admin_widgets: w.place_forget() self.init() def init(self): self.lbl_title.configure(text="{} - {}".format(self.my_gun.gun_id, self.my_gun.gun_mac)) self.lbl_fired.configure(text=str(self.my_gun.fired + self.my_gun.fired_overall)) self.lbl_fired_hist.configure(text=str(self.my_gun.history_fired)) self.lbl_time.configure(text=g.minutes_to_string(self.my_gun.battery + self.my_gun.battery_overall)) self.lbl_time_hist.configure(text=g.minutes_to_string(self.my_gun.history_battery_usage)) self.txt_history.configure(state="normal") self.txt_history.delete("1.0", "end") for item in self.my_gun.change_history: self.txt_history.insert("end", item + "\n") self.txt_history.configure(state="disabled") self.focus_set() def _btn_change_batteries(self): now = datetime.now() self.my_gun.battery_last_changed = now self.my_gun.change_history.append( "{},B,{},F,{}".format(now, self.my_gun.battery + self.my_gun.battery_overall, self.my_gun.fired + self.my_gun.fired_overall)) self.my_gun.history_battery_changes += 1 self.my_gun.history_battery_usage = (self.my_gun.battery + self.my_gun.battery_overall + self.my_gun.history_battery_usage) self.my_gun.history_fired += (self.my_gun.fired + self.my_gun.fired_overall) self.my_gun.battery = 0 self.my_gun.battery_overall = 0 self.my_gun.fired = 0 self.my_gun.fired_overall = 0 self.my_gun.store_gun_info() self.destroy() def _btn_set_fired(self): self.my_gun.fired = int(self.txt_set_fired.get()) self.my_gun.store_gun_info() def _btn_set_time(self): self.my_gun.battery = int(self.txt_set_time.get()) self.my_gun.store_gun_info()