@objc private func showInfo() let alert = NSAlert() alert.messageText = "Battery Status" alert.informativeText = "Current charge: \(getBatteryPercentage())" alert.runModal()
// App delegate class AppDelegate: NSObject, NSApplicationDelegate private var batteryStatusItem: BatteryStatusItem? add battery icon to taskbar
Here's a comprehensive guide to add a battery icon to the taskbar, including multiple implementation approaches. Windows (Native) Method 1: Via Settings (Windows 10/11) # Enable battery icon via registry reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "HideSCABattery" /t REG_DWORD /d 0 /f Restart explorer to apply changes Stop-Process -Name explorer -Force Method 2: PowerShell Script # Show battery icon in taskbar $registryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" Enable battery icon Set-ItemProperty -Path $registryPath -Name "HideSCABattery" -Value 0 -Type DWord Restart taskbar Get-Process explorer | Stop-Process Start-Process explorer Custom Taskbar Battery Icon (Python) import psutil import tkinter as tk from tkinter import ttk import pystray from PIL import Image, ImageDraw import threading class BatteryTrayIcon: def init (self): self.icon = None self.create_tray_icon() @objc private func showInfo() let alert = NSAlert() alert
private func setupMenu() let menu = NSMenu() menu.addItem(NSMenuItem(title: "Battery Info", action: #selector(showInfo), keyEquivalent: "")) menu.addItem(NSMenuItem.separator()) menu.addItem(NSMenuItem(title: "Quit", action: #selector(quit), keyEquivalent: "q")) statusItem?.menu = menu 12)) label.pack(pady=20) progress = ttk.Progressbar(root
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 update_icon(self): percent, is_charging = self.get_battery_info() if percent is not None: icon_name = self.get_icon_name(percent, is_charging) self.indicator.set_icon(icon_name) # Update menu items for item in self.menu.get_children(): if item.get_label() == "Battery Percentage": item.set_label(f"Battery Percentage: percent%") elif item.get_label() == "Status": status = "Charging" if is_charging else "Discharging" item.set_label(f"Status: status") # Update every minute threading.Timer(60, self.update_icon).start()
Choose the implementation that matches your operating system and requirements!