Added predefined right click menu for exiting and self-editing. Removed older "RAM_Used" demo.

This commit is contained in:
PySimpleGUI 2021-05-18 09:54:12 -04:00
parent ed11316f18
commit 26ef16ca23
5 changed files with 21 additions and 82 deletions

View File

@ -256,7 +256,7 @@ class Gauge():
self.pointer.new(degree=self.pointer.stop_degree)
return True
ALPHA = 0.8
ALPHA = 0.5
THEME = 'Dark purple 6 '
UPDATE_FREQUENCY_MILLISECONDS = 2 * 1000
@ -274,7 +274,7 @@ def main(location):
[sg.T(size=(5, 1), font='Any 20', justification='c', background_color='black', k='-gauge VALUE-')]]
window = sg.Window('CPU Usage Widget Square', layout, location=location, no_titlebar=True, grab_anywhere=True, margins=(0, 0), element_padding=(0, 0), alpha_channel=ALPHA, background_color='black', element_justification='c', finalize=True, right_click_menu=[[''], 'Exit'])
window = sg.Window('CPU Usage Widget Square', layout, location=location, no_titlebar=True, grab_anywhere=True, margins=(0, 0), element_padding=(0, 0), alpha_channel=ALPHA, background_color='black', element_justification='c', finalize=True, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_EXIT)
gauge = Gauge(pointer_color=sg.theme_text_color(), clock_color=sg.theme_text_color(), major_tick_color=sg.theme_text_color(),
minor_tick_color=sg.theme_input_background_color(), pointer_outer_color=sg.theme_text_color(), major_tick_start_radius=45,
@ -295,6 +295,8 @@ def main(location):
event, values = window.read(timeout=UPDATE_FREQUENCY_MILLISECONDS)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == 'Edit Me':
sg.execute_editor(__file__)
window.close()

View File

@ -23,12 +23,11 @@ def human_size(bytes, units=(' bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB')):
return str(bytes) + ' ' + units[0] if bytes < 1024 else human_size(bytes >> 10, units[1:])
sg.theme(THEME)
def main(location):
graph = sg.Graph(GSIZE, (0, 0), GSIZE, key='-GRAPH-', enable_events=True)
layout = [[graph]]
window = sg.Window('CPU Usage Widget Square', layout, location=location, no_titlebar=True, grab_anywhere=True, margins=(0, 0), element_padding=(0, 0), alpha_channel=ALPHA, finalize=True, right_click_menu=[[''], 'Exit'])
window = sg.Window('CPU Usage Widget Square', layout, location=location, no_titlebar=True, grab_anywhere=True, margins=(0, 0), element_padding=(0, 0), alpha_channel=ALPHA, finalize=True, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_EXIT)
text_id2 = graph.draw_text(f'CPU', (GSIZE[0] // 2, GSIZE[1] // 4), font='Any 20', text_location=sg.TEXT_LOCATION_CENTER, color=sg.theme_button_color()[0])
@ -41,7 +40,6 @@ def main(location):
rect_id = graph.draw_rectangle((0, rect_height), (GSIZE[0], 0), fill_color=sg.theme_button_color()[1], line_width=0)
# Draw the % used text and the close "X" on bottom
text_id1 = graph.draw_text(f'{int(cpu_percent)}%', (GSIZE[0] // 2, GSIZE[1] // 2), font='Any 40', text_location=sg.TEXT_LOCATION_CENTER, color=sg.theme_button_color()[0])
# text_id3 = graph.draw_text('❎', (0, 0), font='Any 8', text_location=sg.TEXT_LOCATION_BOTTOM_LEFT, color=sg.theme_button_color()[0])
# put the bar behind everything else
graph.send_figure_to_back(rect_id)
@ -49,17 +47,16 @@ def main(location):
event, values = window.read(timeout=UPDATE_FREQUENCY_MILLISECONDS)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == '-GRAPH-': # exit if clicked in the bottom left 20 x 20 pixel area
if values['-GRAPH-'][0] < 20 and values['-GRAPH-'][1] < 20:
break
if event == 'Edit Me':
sg.execute_editor(__file__)
# erase figures so they can be redrawn
graph.delete_figure(rect_id)
graph.delete_figure(text_id1)
# graph.delete_figure(text_id3)
window.close()
if __name__ == '__main__':
sg.theme(THEME)
if len(sys.argv) > 1:
location = sys.argv[1].split(',')

View File

@ -17,7 +17,7 @@ import math
Copyright 2020 PySimpleGUI.org
"""
ALPHA = 0.8
ALPHA = 0.5
THEME = 'Dark Green 5'
UPDATE_FREQUENCY_MILLISECONDS = 2 * 1000
@ -90,11 +90,9 @@ class Gauge():
x, y, r, start, stop, fill, line, width = self.all
start, stop = (180 - start, 180 - stop) if stop < start else (180 - stop, 180 - start)
if start == stop % 360:
self.figure.append(self.graph_elem.DrawCircle((x, y), r, fill_color=fill,
line_color=line, line_width=width))
self.figure.append(self.graph_elem.DrawCircle((x, y), r, fill_color=fill, line_color=line, line_width=width))
else:
self.figure.append(self.graph_elem.DrawArc((x - r, y + r), (x + r, y - r), stop - start,
start, style='arc', arc_color=fill))
self.figure.append(self.graph_elem.DrawArc((x - r, y + r), (x + r, y - r), stop - start, start, style='arc', arc_color=fill))
def move(self, delta_x, delta_y):
"""
@ -147,11 +145,8 @@ class Gauge():
dy1 = int(2 * inner_radius * math.cos(d / 180 * math.pi))
dx2 = int(outer_radius * math.sin(d / 180 * math.pi))
dy2 = int(outer_radius * math.cos(d / 180 * math.pi))
self.figure.append(self.graph_elem.DrawLine((center_x - dx1, center_y - dy1),
(center_x + dx2, center_y + dy2),
color=pointer_color, width=line_width))
self.figure.append(self.graph_elem.DrawCircle((center_x, center_y), inner_radius,
fill_color=origin_color, line_color=outer_color, line_width=line_width))
self.figure.append(self.graph_elem.DrawLine((center_x - dx1, center_y - dy1), (center_x + dx2, center_y + dy2), color=pointer_color, width=line_width))
self.figure.append(self.graph_elem.DrawCircle((center_x, center_y), inner_radius, fill_color=origin_color, line_color=outer_color, line_width=line_width))
def move(self, delta_x, delta_y):
"""
@ -198,8 +193,7 @@ class Gauge():
start_y = y + start_radius * math.sin(i / 180 * math.pi)
stop_x = x + stop_radius * math.cos(i / 180 * math.pi)
stop_y = y + stop_radius * math.sin(i / 180 * math.pi)
self.figure.append(self.graph_elem.DrawLine((start_x, start_y),
(stop_x, stop_y), color=line_color, width=line_width))
self.figure.append(self.graph_elem.DrawLine((start_x, start_y), (stop_x, stop_y), color=line_color, width=line_width))
def move(self, delta_x, delta_y):
"""
@ -281,7 +275,7 @@ def main(location):
[sg.T(size=(8, 1), font='Any 14', justification='c', background_color='black', k='-RAM USED-')],
]
window = sg.Window('CPU Usage Widget Square', layout, location=location, no_titlebar=True, grab_anywhere=True, margins=(0, 0), element_padding=(0, 0), alpha_channel=ALPHA, background_color='black', element_justification='c', finalize=True, right_click_menu=[[''], 'Exit'])
window = sg.Window('CPU Usage Widget Square', layout, location=location, no_titlebar=True, grab_anywhere=True, margins=(0, 0), element_padding=(0, 0), alpha_channel=ALPHA, background_color='black', element_justification='c', finalize=True, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_EXIT)
gauge = Gauge(pointer_color=sg.theme_text_color(), clock_color=sg.theme_text_color(), major_tick_color=sg.theme_text_color(),
minor_tick_color=sg.theme_input_background_color(), pointer_outer_color=sg.theme_text_color(), major_tick_start_radius=45,
@ -306,7 +300,8 @@ def main(location):
event, values = window.read(timeout=UPDATE_FREQUENCY_MILLISECONDS)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == 'Edit Me':
sg.execute_editor(__file__)
window.close()

View File

@ -30,7 +30,7 @@ def main(location):
graph = sg.Graph(GSIZE, (0, 0), GSIZE, key='-GRAPH-', enable_events=True)
layout = [[graph]]
window = sg.Window('RAM Usage Widget Square', layout, location=location, no_titlebar=True, grab_anywhere=True, margins=(0, 0), element_padding=(0, 0), alpha_channel=ALPHA, finalize=True, right_click_menu=[[''], 'Exit'])
window = sg.Window('RAM Usage Widget Square', layout, location=location, no_titlebar=True, grab_anywhere=True, margins=(0, 0), element_padding=(0, 0), alpha_channel=ALPHA, finalize=True, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_EXIT)
while True: # Event Loop
@ -41,18 +41,16 @@ def main(location):
text_id1 = graph.draw_text(f'{int(ram.percent)}%', (GSIZE[0] // 2, GSIZE[1] // 2), font='Any 40', text_location=sg.TEXT_LOCATION_CENTER,
color=sg.theme_button_color()[0])
text_id2 = graph.draw_text(f'{human_size(ram.used)} used', (GSIZE[0] // 2, GSIZE[1] // 4), font='Any 20', text_location=sg.TEXT_LOCATION_CENTER, color=sg.theme_button_color()[0])
# text_id3 = graph.draw_text('❎', (0, 0), font='Any 8', text_location=sg.TEXT_LOCATION_BOTTOM_LEFT, color=sg.theme_button_color()[0])
event, values = window.read(timeout=UPDATE_FREQUENCY_MILLISECONDS)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == '-GRAPH-': # exit if clicked in the bottom left 20 x 20 pixel area
if values['-GRAPH-'][0] < 20 and values['-GRAPH-'][1] < 20:
break
elif event == 'Edit Me':
sg.execute_editor(__file__)
graph.delete_figure(rect_id)
graph.delete_figure(text_id1)
graph.delete_figure(text_id2)
# graph.delete_figure(text_id3)
window.close()
if __name__ == '__main__':

View File

@ -1,53 +0,0 @@
import PySimpleGUI as sg
import psutil
"""
Another simple Desktop Widget using PySimpleGUI
This time a RAM indicator. The Widget is square. The bottom section will be shaded to
represent the total amount of RAM currently in use.
The % and number of bytes in use is shown on top in text.
Uses the theme's button color for colors.
Copyright 2020 PySimpleGUI.org
"""
ALPHA = 0.5
THEME = 'Dark Green 5'
GSIZE = (160, 160)
UPDATE_FREQUENCY_MILLISECONDS = 1 * 1000
def human_size(bytes, units=(' bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB')):
""" Returns a human readable string reprentation of bytes"""
return str(bytes) + ' ' + units[0] if bytes < 1024 else human_size(bytes >> 10, units[1:])
sg.theme(THEME)
graph = sg.Graph(GSIZE, (0, 0), GSIZE, key='-GRAPH-', enable_events=True)
layout = [[graph]]
window = sg.Window('RAM Usage', layout, no_titlebar=True, grab_anywhere=True, margins=(0, 0), element_padding=(0, 0), alpha_channel=ALPHA, finalize=True)
while True: # Event Loop
# ----------- update the graphics and text in the window ------------
ram = psutil.virtual_memory()
rect_height = int(GSIZE[1] * float(ram.percent) / 100)
rect_id = graph.draw_rectangle((0, rect_height), (GSIZE[0], 0), fill_color=sg.theme_button_color()[1], line_width=0)
text_id1 = graph.draw_text(f'{int(ram.percent)}%', (GSIZE[0] // 2, GSIZE[1] // 2), font='Any 40', text_location=sg.TEXT_LOCATION_CENTER,
color=sg.theme_button_color()[0])
text_id2 = graph.draw_text(f'{human_size(ram.used)} used', (GSIZE[0] // 2, GSIZE[1] // 4), font='Any 20', text_location=sg.TEXT_LOCATION_CENTER,
color=sg.theme_button_color()[0])
text_id3 = graph.draw_text('', (0, 0), font='Any 8', text_location=sg.TEXT_LOCATION_BOTTOM_LEFT, color=sg.theme_button_color()[0])
event, values = window.read(timeout=UPDATE_FREQUENCY_MILLISECONDS)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == '-GRAPH-': # exit if clicked in the bottom left 20 x 20 pixel area
if values['-GRAPH-'][0] < 20 and values['-GRAPH-'][1] < 20:
break
graph.delete_figure(rect_id)
graph.delete_figure(text_id1)
graph.delete_figure(text_id2)
graph.delete_figure(text_id3)
window.close()