2018-09-27 20:24:09 +00:00
#!/usr/bin/env python
2019-10-23 20:10:03 +00:00
import PySimpleGUI as sg
2018-09-12 20:46:35 +00:00
import psutil
2018-09-13 21:24:55 +00:00
import operator
2018-09-14 15:54:10 +00:00
2018-09-12 20:46:35 +00:00
"""
2022-03-08 15:03:32 +00:00
PSUTIL " Top " CPU Processes - Desktop Widget
Creates a floating CPU utilization window running something similar to a " top " command .
2018-09-12 20:46:35 +00:00
2022-03-08 15:03:32 +00:00
Use the spinner to adjust the number of seconds between readings of the CPU utilizaiton
Rather than calling the threading module this program uses the PySimpleGUI perform_long_operation method .
The result is similar . The function is run as a thread . . . the call is simply wrapped .
Copyright 2022 PySimpleGUI
2018-09-12 20:46:35 +00:00
"""
2022-03-08 15:03:32 +00:00
# global used to communicate with thread.
g_interval = 1 # how often to poll for CPU usage
def CPU_thread ( window : sg . Window ) :
while True :
cpu_percent = psutil . cpu_percent ( interval = g_interval )
procs = psutil . process_iter ( )
2022-03-08 15:16:58 +00:00
window . write_event_value ( ' -CPU UPDATE FROM THREAD- ' , ( cpu_percent , procs ) )
2018-09-14 15:54:10 +00:00
2022-03-08 15:03:32 +00:00
def main ( ) :
global g_interval
2018-09-14 15:54:10 +00:00
2022-03-08 15:03:32 +00:00
location = sg . user_settings_get_entry ( ' -location- ' , ( None , None ) )
2018-09-14 15:54:10 +00:00
# ---------------- Create Form ----------------
2019-12-24 23:52:47 +00:00
sg . theme ( ' Black ' )
2022-03-08 15:16:58 +00:00
2022-04-17 13:58:58 +00:00
layout = [ [ sg . Text ( font = ( ' Helvetica ' , 20 ) , text_color = sg . YELLOWS [ 0 ] , key = ' -CPU PERCENT- ' ) ] ,
2022-03-08 15:16:58 +00:00
[ sg . Text ( size = ( 35 , 12 ) , font = ( ' Courier New ' , 12 ) , key = ' -PROCESSES- ' ) ] , # size will determine how many processes shown
2022-04-17 13:58:58 +00:00
[ sg . Text ( ' Update every ' ) , sg . Spin ( [ x + 1 for x in range ( 10 ) ] , 3 , key = ' -SPIN- ' ) , sg . T ( ' seconds ' ) ] ]
2019-10-23 20:10:03 +00:00
2022-04-17 13:58:58 +00:00
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_VER_EXIT , enable_close_attempted_event = True )
2018-09-24 22:01:00 +00:00
2018-09-15 19:44:57 +00:00
# start cpu measurement thread
2022-03-08 15:03:32 +00:00
# using the PySimpleGUI call to start and manage the thread
window . perform_long_operation ( lambda : CPU_thread ( window ) , ' -THREAD FINISHED- ' )
2018-10-18 18:56:08 +00:00
g_interval = 1
2018-09-14 15:54:10 +00:00
# ---------------- main loop ----------------
2019-10-23 20:10:03 +00:00
while True :
2018-09-14 15:54:10 +00:00
# --------- Read and update window --------
2022-03-08 15:03:32 +00:00
event , values = window . read ( )
2022-04-17 13:58:58 +00:00
# print(event, values)
2018-09-14 15:54:10 +00:00
# --------- Do Button Operations --------
2022-03-08 15:03:32 +00:00
if event in ( sg . WIN_CLOSE_ATTEMPTED_EVENT , ' Exit ' ) :
sg . user_settings_set_entry ( ' -location- ' , window . current_location ( ) ) # save window location before exiting
2018-09-14 15:54:10 +00:00
break
2022-04-17 13:58:58 +00:00
if event == ' Edit Me ' :
sp = sg . execute_editor ( __file__ )
elif event == ' Version ' :
sg . popup_scrolled ( __file__ , sg . get_versions ( ) , keep_on_top = True , location = window . current_location ( ) )
2022-03-08 15:16:58 +00:00
elif event == ' -CPU UPDATE FROM THREAD- ' : # indicates data from the thread has arrived
2022-04-17 13:58:58 +00:00
cpu_percent , procs = values [ event ] # the thread sends a tuple
2022-03-08 15:03:32 +00:00
if procs :
# --------- Create dictionary of top % CPU processes. Format is name:cpu_percent --------
top = { proc . name ( ) : proc . cpu_percent ( ) for proc in procs }
2018-09-14 15:54:10 +00:00
2022-03-08 15:03:32 +00:00
top_sorted = sorted ( top . items ( ) , key = operator . itemgetter ( 1 ) , reverse = True ) # reverse sort to get highest CPU usage on top
if top_sorted :
top_sorted . pop ( 0 ) # remove the idle process
2022-03-08 15:16:58 +00:00
display_string = ' \n ' . join ( [ f ' { cpu / 10 : 2.2f } { proc : 23 } ' for proc , cpu in top_sorted ] )
2022-03-08 15:03:32 +00:00
# --------- Display timer and proceses in window --------
2022-03-08 15:16:58 +00:00
window [ ' -CPU PERCENT- ' ] . update ( f ' CPU { cpu_percent } ' )
window [ ' -PROCESSES- ' ] . update ( display_string )
2022-03-08 15:03:32 +00:00
# get the timeout from the spinner
2022-04-17 13:58:58 +00:00
g_interval = int ( values [ ' -SPIN- ' ] )
2018-09-14 15:54:10 +00:00
2019-10-23 20:10:03 +00:00
window . close ( )
2020-07-28 08:11:25 +00:00
if __name__ == ' __main__ ' :
2022-03-08 15:03:32 +00:00
main ( )