ProyectoFinalPython/app/widgets/UsageLabels.py

51 lines
1.3 KiB
Python

import time
import psutil
from .abc import ThreadedLabel
class CPULabel(ThreadedLabel):
def task(self, *args):
cpu_percent = psutil.cpu_percent()
self.config(text=f'CPU: {cpu_percent}%')
class RAMLabel(ThreadedLabel):
def task(self, *args):
memory = psutil.virtual_memory()
self.config(text=f'RAM: {memory.percent}%')
class BatteryLabel(ThreadedLabel):
def task(self, *args):
battery = psutil.sensors_battery()
if battery is None:
self.config(text='Battery: N/A')
return
battery_percent = battery.percent
is_charging = battery.power_plugged
time_left = battery.secsleft
text = f'Battery: {battery_percent:.0f}%'
if is_charging:
text += ', Plugged in'
else:
text += f', ({time_left // 3600}h {time_left % 3600 // 60}m left)'
try:
self.config(text=text)
except RuntimeError:
pass # Catch update on closed widget
time.sleep(1)
class NetworkLabel(ThreadedLabel):
def task(self, *args):
network = psutil.net_io_counters()
self.config(text=f'Net: {network.bytes_sent / 1024 / 1024:.2f} MB snt,'
f' {network.bytes_recv / 1024 / 1024:.2f} MB rcv')