11#!/usr/bin/env python
22import PySimpleGUI as sg
33import psutil
4- import time
54import operator
6- import sys
75
86"""
97 PSUTIL "Top" CPU Processes - Desktop Widget
@@ -25,22 +23,20 @@ def CPU_thread(window:sg.Window):
2523 while True :
2624 cpu_percent = psutil .cpu_percent (interval = g_interval )
2725 procs = psutil .process_iter ()
28- window .write_event_value ('-CPU UPDATE-' , (cpu_percent , procs ))
26+ window .write_event_value ('-CPU UPDATE FROM THREAD -' , (cpu_percent , procs ))
2927
3028
3129def main ():
3230 global g_interval
3331
3432 location = sg .user_settings_get_entry ('-location-' , (None , None ))
3533
36-
3734 # ---------------- Create Form ----------------
3835 sg .theme ('Black' )
39- layout = [[sg .Text ('' , size = (8 , 1 ), font = ('Helvetica' , 20 ),
40- text_color = sg .YELLOWS [0 ], justification = 'center' , key = 'text' ), sg .Push (), sg .Text ('❎' , enable_events = True , key = 'Exit' )],
41- [sg .Text ('' , size = (30 , 8 ), font = ('Courier New' , 12 ),
42- text_color = 'white' , justification = 'left' , key = 'processes' )],
43- [sg .Text ('Update every ' ), sg .Spin ([x + 1 for x in range (10 )], 3 , key = 'spin' ), sg .T ('seconds ' )]]
36+
37+ layout = [[sg .Text (font = ('Helvetica' , 20 ), text_color = sg .YELLOWS [0 ], key = '-CPU PERCENT-' ), sg .Push (), sg .Text (sg .SYMBOL_X , enable_events = True , key = 'Exit' )],
38+ [sg .Text (size = (35 , 12 ), font = ('Courier New' , 12 ), key = '-PROCESSES-' )], # size will determine how many processes shown
39+ [sg .Text ('Update every ' ), sg .Spin ([x + 1 for x in range (10 )], 3 , key = 'spin' ), sg .T ('seconds' )]]
4440
4541 window = sg .Window ('Top CPU Processes' , layout , no_titlebar = True , keep_on_top = True ,location = location , use_default_focus = False , alpha_channel = .8 , grab_anywhere = True , right_click_menu = sg .MENU_RIGHT_CLICK_EDITME_EXIT , enable_close_attempted_event = True )
4642
@@ -58,7 +54,7 @@ def main():
5854 break
5955 elif event == 'Edit Me' :
6056 sg .execute_editor (__file__ )
61- elif event == '-CPU UPDATE-' : # indicates data from the thread has arrived
57+ elif event == '-CPU UPDATE FROM THREAD -' : # indicates data from the thread has arrived
6258 cpu_percent , procs = values [event ] # the thread sends a tuple
6359 if procs :
6460 # --------- Create dictionary of top % CPU processes. Format is name:cpu_percent --------
@@ -67,10 +63,10 @@ def main():
6763 top_sorted = sorted (top .items (), key = operator .itemgetter (1 ), reverse = True ) # reverse sort to get highest CPU usage on top
6864 if top_sorted :
6965 top_sorted .pop (0 ) # remove the idle process
70- display_string = '\n ' .join ([f'{ cpu / 10 :2.2f} { proc } ' for proc , cpu in top_sorted ])
66+ display_string = '\n ' .join ([f'{ cpu / 10 :2.2f} { proc :23 } ' for proc , cpu in top_sorted ])
7167 # --------- Display timer and proceses in window --------
72- window ['text ' ].update (f'CPU { cpu_percent } ' )
73- window ['processes ' ].update (display_string )
68+ window ['-CPU PERCENT- ' ].update (f'CPU { cpu_percent } ' )
69+ window ['-PROCESSES- ' ].update (display_string )
7470 # get the timeout from the spinner
7571 g_interval = int (values ['spin' ])
7672
0 commit comments