add battery icon to taskbar
BVEStation

Add Battery Icon To Taskbar →

def show_battery_info(self): percent, is_charging = self.get_battery_status() status = f"Battery: percent%\n" status += "Charging" if is_charging else "Discharging" # Create popup window root = tk.Tk() root.title("Battery Status") root.geometry("300x150") label = tk.Label(root, text=status, font=("Arial", 12)) label.pack(pady=20) progress = ttk.Progressbar(root, length=200, mode='determinate') progress['value'] = percent progress.pack(pady=10) tk.Button(root, text="Close", command=root.destroy).pack(pady=10) root.mainloop()

def exit_app(self): if self.icon: self.icon.stop() if == " main ": BatteryTrayIcon() Linux (GTK) #!/usr/bin/env python3 import gi gi.require_version('Gtk', '3.0') gi.require_version('AppIndicator3', '0.1') from gi.repository import Gtk, AppIndicator3 import psutil import threading import time class BatteryIndicator: def init (self): self.indicator = AppIndicator3.Indicator.new( "battery-indicator", "battery-full", AppIndicator3.IndicatorCategory.SYSTEM_SERVICES ) self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)

private func startMonitoring() timer = Timer.scheduledTimer(withTimeInterval: 60, repeats: true) [weak self] _ in DispatchQueue.main.async self?.statusItem?.button?.title = self?.getBatteryPercentage() ?? "??%" add battery icon to taskbar

def create_battery_icon(self, percent, is_charging): # Create icon image size = 64 image = Image.new('RGBA', (size, size), (0, 0, 0, 0)) draw = ImageDraw.Draw(image) # Battery outline draw.rectangle( [(8, 20), (56, 44)], outline='white', width=2 ) # Battery terminal draw.rectangle( [(56, 28), (60, 36)], fill='white' ) # Battery level fill_width = int(48 * (percent / 100)) draw.rectangle( [(10, 22), (10 + fill_width, 42)], fill='#00ff00' if percent > 20 else '#ff0000' ) # Charging indicator if is_charging: draw.line([(28, 12), (36, 12)], fill='#00ff00', width=2) draw.line([(32, 12), (32, 20)], fill='#00ff00', width=2) return image

@objc private func toggleMenu() statusItem?.menu?.popUp(positioning: nil, at: NSEvent.mouseLocation, in: nil) def show_battery_info(self): percent, is_charging = self

self.menu = Gtk.Menu() self.create_menu() self.indicator.set_menu(self.menu) self.update_icon() def get_battery_info(self): battery = psutil.sensors_battery() if battery: percent = battery.percent is_charging = battery.power_plugged return percent, is_charging return None, None

def get_battery_status(self): battery = psutil.sensors_battery() if battery: percent = battery.percent is_charging = battery.power_plugged return percent, is_charging return None, None def show_battery_info(self): percent

def quit(self, source): Gtk.main_quit() if == " main ": indicator = BatteryIndicator() Gtk.main() macOS (Swift) import Cocoa import IOKit.ps class BatteryStatusItem: NSObject private var statusItem: NSStatusItem? private var timer: Timer?