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()