Updated demo to use newer coding guidelines, better key format

This commit is contained in:
PySimpleGUI 2021-03-07 13:01:42 -05:00
parent 62fbb19bbd
commit 923723f7a7
1 changed files with 20 additions and 17 deletions

View File

@ -13,6 +13,8 @@ import psutil
An alpha-channel setting of 0.8 to give a little transparency
No titlebar
Grab anywhere, making window easy to move around
Note that the keys are tuples, with a tuple as the second item
('-KEY-', (row, col))
Copyright 2020 PySimpleGUI
"""
@ -37,9 +39,9 @@ class DashGraph(object):
def graph_percentage_abs(self, value):
self.line_list.append(self.graph_elem.draw_line( # draw a line and add to list of lines
(self.graph_current_item, 0),
(self.graph_current_item, value),
color=self.color))
(self.graph_current_item, 0),
(self.graph_current_item, value),
color=self.color))
if self.graph_current_item >= GRAPH_WIDTH:
self.graph_elem.move(-1,0)
self.graph_elem.delete_figure(self.line_list[0]) # delete the oldest line
@ -56,41 +58,42 @@ def main(location):
return(sg.Text(text, font=('Helvetica 8'), **kwargs))
def GraphColumn(name, key):
return sg.Column([[Txt(name, size=(10,1), key=key+'_TXT_')],
[sg.Graph((GRAPH_WIDTH, GRAPH_HEIGHT), (0, 0), (GRAPH_WIDTH, 100), background_color='black', key=key+'_GRAPH_')]], pad=(2, 2))
return sg.Column([[Txt(name, size=(10,1), key=('-TXT-', key))],
[sg.Graph((GRAPH_WIDTH, GRAPH_HEIGHT), (0, 0), (GRAPH_WIDTH, 100), background_color='black', key=('-GRAPH-', key))]], pad=(2, 2))
num_cores = len(psutil.cpu_percent(percpu=True)) # get the number of cores in the CPU
sg.theme('Black')
sg.set_options(element_padding=(0,0), margins=(1,1), border_width=0)
# the clever Red X graphic
red_x = "R0lGODlhEAAQAPeQAIsAAI0AAI4AAI8AAJIAAJUAAJQCApkAAJoAAJ4AAJkJCaAAAKYAAKcAAKcCAKcDA6cGAKgAAKsAAKsCAKwAAK0AAK8AAK4CAK8DAqUJAKULAKwLALAAALEAALIAALMAALMDALQAALUAALYAALcEALoAALsAALsCALwAAL8AALkJAL4NAL8NAKoTAKwbAbE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MLtgUocJFy5orL0IQRHAiQgsbRZYswbEhBIiCCH6EiJAhAwQMKU5DjHCi9gnZEHMTDAgAOw=="
layout = [[ sg.Button(image_data=red_x, button_color=('black', 'black'), key='Exit', tooltip='Closes window'),
layout = [[ sg.Text(sg.SYMBOL_X, enable_events=True, key='Exit', tooltip='Closes window'),
sg.Text(' CPU Core Usage')] ]
# add on the graphs
for rows in range(num_cores//NUM_COLS+1):
# for cols in range(min(num_cores-rows*NUM_COLS, NUM_COLS)):
layout += [[GraphColumn('CPU '+str(rows*NUM_COLS+cols), '_CPU_'+str(rows*NUM_COLS+cols)) for cols in range(min(num_cores-rows*NUM_COLS, NUM_COLS))]]
layout += [[GraphColumn('CPU '+str(rows*NUM_COLS+cols), (rows, cols)) for cols in range(min(num_cores-rows*NUM_COLS, NUM_COLS))]]
# ---------------- Create Window ----------------
window = sg.Window('CPU Cores Usage Widget', layout,
keep_on_top=True,
auto_size_buttons=False,
grab_anywhere=True,
no_titlebar=True,
default_button_element_size=(12, 1),
return_keyboard_events=True,
alpha_channel=TRANSPARENCY,
use_default_focus=False,
finalize=True,
margins=(1,1),
element_padding=(0,0),
border_depth=0,
location=location)
# setup graphs & initial values
graphs = [DashGraph(window['_CPU_'+str(i)+'_GRAPH_'],
window['_CPU_'+str(i) + '_TXT_'],
0, colors[i%6]) for i in range(num_cores) ]
graphs = []
for rows in range(num_cores//NUM_COLS+1):
for cols in range(min(num_cores-rows*NUM_COLS, NUM_COLS)):
graphs += [DashGraph(window[('-GRAPH-', (rows, cols))],
window[('-TXT-', (rows, cols))],
0, colors[(rows*NUM_COLS+cols)%len(colors)])]
# ---------------- main loop ----------------
while True :