commit c55e87a452d6d2e680ae117e23c72db05861b2db Author: suti7yk5032 Date: Sun May 12 22:15:56 2024 +0900 1st diff --git a/battery_checker.py b/battery_checker.py new file mode 100644 index 0000000..240d849 --- /dev/null +++ b/battery_checker.py @@ -0,0 +1,32 @@ +import xml.etree.ElementTree as ET +import subprocess +import os + +def get(filename): + if os.path.isfile(filename): + os.remove(filename) + + try: + detail = subprocess.run(["powercfg", "/batteryreport", "/output", filename, "/XML"], check=True) + return 0 + except: + return 1 + +def parse(filename): + tree = ET.parse(filename) + root = tree.getroot() + + capacity = {"design": int(root[2][0][7].text), + "current": int(root[2][0][8].text), + "cycle": int(root[2][0][9].text)} + + return capacity + +xmlfile = "battery.xml" + +get_detail = get(xmlfile) +if get_detail == 0: + battery_about = parse(xmlfile) + print("電池の容量 : " + str(round(battery_about["current"] / battery_about["design"] * 100, 1)) + " %") +else: + print("エラー") \ No newline at end of file diff --git a/battery_checker_gui.py b/battery_checker_gui.py new file mode 100644 index 0000000..edfcc25 --- /dev/null +++ b/battery_checker_gui.py @@ -0,0 +1,75 @@ +import xml.etree.ElementTree as ET +import subprocess +import os +import customtkinter + +class Battery(): + def get(self, filename): + if os.path.isfile(filename): + os.remove(filename) + + try: + detail = subprocess.run(["powercfg", "/batteryreport", "/output", filename, "/XML"], check=True) + return 0 + except: + return 1 + + def parse(self, filename): + tree = ET.parse(filename) + root = tree.getroot() + + capacity = {"design": int(root[2][0][7].text), + "current": int(root[2][0][8].text), + "cycle": int(root[2][0][9].text)} + + return capacity + +class App(customtkinter.CTk): + def __init__(self): + super().__init__() + + appname = "ばってりーちぇっかー" + frame_padx = 10 + frame_pady = 10 + + self.title = appname + self.geometry("360x500") + self.resizable(height=False, width=False) + + self.frame = customtkinter.CTkFrame(self, corner_radius=0) + self.frame.grid(row=0, column=0, sticky="nsew") + self.frame.grid_rowconfigure(4, weight=1) + + label_w = 300 + label_padx = 20 + label_pady = 20 + + self.battery_current_capacity_frame = customtkinter.CTkFrame(self.frame, fg_color="transparent") + self.battery_current_capacity_frame.grid(row=0, column=0, padx=frame_padx, pady=frame_pady) + + self.battery_current_capacity_title = customtkinter.CTkLabel(self.battery_current_capacity_frame, text="現在のバッテリーの状態", width=label_w) + self.battery_current_capacity_title.grid(row=0, column=0, padx=label_padx, pady=label_pady) + + self.battery_current_capacity_detail = customtkinter.CTkLabel(self.battery_current_capacity_frame, text="計測中...", width=label_w) + self.battery_current_capacity_detail.grid(row=1, column=0, padx=label_padx, pady=label_pady) + + def bat_get(): + bat = Battery() + + xmlfile = "battery.xml" + + get_detail = bat.get(xmlfile) + if get_detail == 0: + battery_about = bat.parse(xmlfile) + self.battery_current_capacity_detail.configure(text=str(round(battery_about["current"] / battery_about["design"] * 100, 1)) + " %") + print("電池の容量 : " + str(round(battery_about["current"] / battery_about["design"] * 100, 1)) + " %") + else: + print("エラー") + + bat_get() + + + +if __name__ == "__main__": + app = App() + app.mainloop() \ No newline at end of file