RELEASE 3.2.0
This commit is contained in:
parent
317f90b0c5
commit
4f43f7bcaf
3 changed files with 91 additions and 36 deletions
|
@ -1,7 +1,10 @@
|
|||
import PySimpleGUI as sg
|
||||
import psutil
|
||||
import time
|
||||
from threading import Thread
|
||||
import operator
|
||||
|
||||
|
||||
"""
|
||||
PSUTIL Desktop Widget
|
||||
Creates a floating CPU utilization window that is always on top of other windows
|
||||
|
@ -14,43 +17,73 @@ import operator
|
|||
invalid command name "1616802625480StopMove"
|
||||
"""
|
||||
|
||||
# ---------------- Create Form ----------------
|
||||
sg.ChangeLookAndFeel('Black')
|
||||
form_rows = [[sg.Text('', size=(8,1), font=('Helvetica', 20),text_color=sg.YELLOWS[0], justification='center', key='text')],
|
||||
[sg.Text('', size=(30, 8), font=('Courier', 10),text_color='white', justification='left', key='processes')],
|
||||
[sg.Exit(button_color=('white', 'firebrick4'), pad=((15,0), 0)), sg.Spin([x+1 for x in range(10)], 1, key='spin')]]
|
||||
|
||||
form = sg.FlexForm('CPU Utilization', no_titlebar=True, auto_size_buttons=False, keep_on_top=True, grab_anywhere=True)
|
||||
form.Layout(form_rows)
|
||||
g_interval = 1
|
||||
g_cpu_percent = 0
|
||||
g_procs = None
|
||||
g_exit = False
|
||||
|
||||
# ---------------- main loop ----------------
|
||||
while (True):
|
||||
# --------- Read and update window --------
|
||||
button, values = form.ReadNonBlocking()
|
||||
def CPU_thread(args):
|
||||
global g_interval, g_cpu_percent, g_procs, g_exit
|
||||
|
||||
# --------- Do Button Operations --------
|
||||
if values is None or button == 'Exit':
|
||||
break
|
||||
try:
|
||||
interval = int(values['spin'])
|
||||
except:
|
||||
interval = 1
|
||||
while not g_exit:
|
||||
g_cpu_percent = psutil.cpu_percent(interval=g_interval)
|
||||
g_procs = psutil.process_iter()
|
||||
|
||||
cpu_percent = psutil.cpu_percent(interval=interval)
|
||||
|
||||
# --------- Create list of top % CPU porocesses --------
|
||||
top = {proc.name() : proc.cpu_percent() for proc in psutil.process_iter()}
|
||||
def main():
|
||||
global g_interval, g_procs, g_exit
|
||||
|
||||
top_sorted = sorted(top.items(), key=operator.itemgetter(1), reverse=True)
|
||||
top_sorted.pop(0)
|
||||
display_string = ''
|
||||
for proc, cpu in top_sorted:
|
||||
display_string += '{} {}\n'.format(cpu, proc)
|
||||
# ---------------- Create Form ----------------
|
||||
sg.ChangeLookAndFeel('Black')
|
||||
form_rows = [[sg.Text('', size=(8,1), font=('Helvetica', 20),text_color=sg.YELLOWS[0], justification='center', key='text')],
|
||||
[sg.Text('', size=(30, 8), font=('Courier New', 12),text_color='white', justification='left', key='processes')],
|
||||
[sg.Exit(button_color=('white', 'firebrick4'), pad=((15,0), 0)), sg.Spin([x+1 for x in range(10)], 1, key='spin')],
|
||||
]
|
||||
|
||||
# --------- Display timer in window --------
|
||||
form.FindElement('text').Update('CPU {}'.format(cpu_percent))
|
||||
# form.FindElement('processes').Update('\n'.join(top_sorted))
|
||||
form.FindElement('processes').Update(display_string)
|
||||
form = sg.FlexForm('CPU Utilization', no_titlebar=True, auto_size_buttons=False, keep_on_top=True, grab_anywhere=True)
|
||||
form.Layout(form_rows)
|
||||
thread = Thread(target=CPU_thread,args=(None,))
|
||||
thread.start()
|
||||
# ---------------- main loop ----------------
|
||||
while (True):
|
||||
# --------- Read and update window --------
|
||||
button, values = form.ReadNonBlocking()
|
||||
|
||||
# Broke out of main loop. Close the window.
|
||||
form.CloseNonBlockingForm()
|
||||
# --------- Do Button Operations --------
|
||||
if values is None or button == 'Exit':
|
||||
break
|
||||
try:
|
||||
g_interval = int(values['spin'])
|
||||
except:
|
||||
g_interval = 1
|
||||
|
||||
# cpu_percent = psutil.cpu_percent(interval=interval)
|
||||
cpu_percent = g_cpu_percent
|
||||
time.sleep(.1)
|
||||
|
||||
display_string = ''
|
||||
if g_procs:
|
||||
# --------- Create list of top % CPU porocesses --------
|
||||
top = {proc.name() : proc.cpu_percent() for proc in g_procs}
|
||||
|
||||
top_sorted = sorted(top.items(), key=operator.itemgetter(1), reverse=True)
|
||||
if top_sorted:
|
||||
top_sorted.pop(0)
|
||||
display_string = ''
|
||||
for proc, cpu in top_sorted:
|
||||
display_string += '{} {}\n'.format(cpu, proc)
|
||||
|
||||
|
||||
# --------- Display timer in window --------
|
||||
form.FindElement('text').Update('CPU {}'.format(cpu_percent))
|
||||
form.FindElement('processes').Update(display_string)
|
||||
|
||||
# Broke out of main loop. Close the window.
|
||||
form.CloseNonBlockingForm()
|
||||
g_exit = True
|
||||
thread.join()
|
||||
exit(69)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue