Add Battery Icon To Taskbar May 2026
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
private func getBatteryPercentage() -> String guard let snapshot = IOPSCopyPowerSourcesInfo()?.takeRetainedValue(), let sources = IOPSCopyPowerSourcesList(snapshot)?.takeRetainedValue() as? [CFTypeRef], let source = sources.first else return "??%" guard let info = IOPSGetPowerSourceDescription(snapshot, source)?.takeUnretainedValue() as? [String: Any] else return "??%" if let currentCapacity = info[kIOPSCurrentCapacityKey] as? Int, let maxCapacity = info[kIOPSMaxCapacityKey] as? Int let percentage = (currentCapacity * 100) / maxCapacity return "\(percentage)%" return "??%" add battery icon to taskbar
// App delegate class AppDelegate: NSObject, NSApplicationDelegate private var batteryStatusItem: BatteryStatusItem? def get_battery_status(self): battery = psutil
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 Int, let maxCapacity = info[kIOPSMaxCapacityKey] as
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)