Added predefined right click menu for exiting and self-editing. Removed older "RAM_Used" demo.
This commit is contained in:
parent
ed11316f18
commit
26ef16ca23
|
@ -256,7 +256,7 @@ class Gauge():
|
||||||
self.pointer.new(degree=self.pointer.stop_degree)
|
self.pointer.new(degree=self.pointer.stop_degree)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
ALPHA = 0.8
|
ALPHA = 0.5
|
||||||
THEME = 'Dark purple 6 '
|
THEME = 'Dark purple 6 '
|
||||||
UPDATE_FREQUENCY_MILLISECONDS = 2 * 1000
|
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-')]]
|
[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(),
|
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,
|
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)
|
event, values = window.read(timeout=UPDATE_FREQUENCY_MILLISECONDS)
|
||||||
if event == sg.WIN_CLOSED or event == 'Exit':
|
if event == sg.WIN_CLOSED or event == 'Exit':
|
||||||
break
|
break
|
||||||
|
if event == 'Edit Me':
|
||||||
|
sg.execute_editor(__file__)
|
||||||
window.close()
|
window.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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:])
|
return str(bytes) + ' ' + units[0] if bytes < 1024 else human_size(bytes >> 10, units[1:])
|
||||||
|
|
||||||
|
|
||||||
sg.theme(THEME)
|
|
||||||
def main(location):
|
def main(location):
|
||||||
graph = sg.Graph(GSIZE, (0, 0), GSIZE, key='-GRAPH-', enable_events=True)
|
graph = sg.Graph(GSIZE, (0, 0), GSIZE, key='-GRAPH-', enable_events=True)
|
||||||
layout = [[graph]]
|
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])
|
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)
|
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
|
# 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_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
|
# put the bar behind everything else
|
||||||
graph.send_figure_to_back(rect_id)
|
graph.send_figure_to_back(rect_id)
|
||||||
|
|
||||||
|
@ -49,17 +47,16 @@ def main(location):
|
||||||
event, values = window.read(timeout=UPDATE_FREQUENCY_MILLISECONDS)
|
event, values = window.read(timeout=UPDATE_FREQUENCY_MILLISECONDS)
|
||||||
if event == sg.WIN_CLOSED or event == 'Exit':
|
if event == sg.WIN_CLOSED or event == 'Exit':
|
||||||
break
|
break
|
||||||
if event == '-GRAPH-': # exit if clicked in the bottom left 20 x 20 pixel area
|
if event == 'Edit Me':
|
||||||
if values['-GRAPH-'][0] < 20 and values['-GRAPH-'][1] < 20:
|
sg.execute_editor(__file__)
|
||||||
break
|
|
||||||
# erase figures so they can be redrawn
|
# erase figures so they can be redrawn
|
||||||
graph.delete_figure(rect_id)
|
graph.delete_figure(rect_id)
|
||||||
graph.delete_figure(text_id1)
|
graph.delete_figure(text_id1)
|
||||||
# graph.delete_figure(text_id3)
|
|
||||||
window.close()
|
window.close()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
sg.theme(THEME)
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
location = sys.argv[1].split(',')
|
location = sys.argv[1].split(',')
|
||||||
|
|
|
@ -17,7 +17,7 @@ import math
|
||||||
Copyright 2020 PySimpleGUI.org
|
Copyright 2020 PySimpleGUI.org
|
||||||
"""
|
"""
|
||||||
|
|
||||||
ALPHA = 0.8
|
ALPHA = 0.5
|
||||||
THEME = 'Dark Green 5'
|
THEME = 'Dark Green 5'
|
||||||
UPDATE_FREQUENCY_MILLISECONDS = 2 * 1000
|
UPDATE_FREQUENCY_MILLISECONDS = 2 * 1000
|
||||||
|
|
||||||
|
@ -90,11 +90,9 @@ class Gauge():
|
||||||
x, y, r, start, stop, fill, line, width = self.all
|
x, y, r, start, stop, fill, line, width = self.all
|
||||||
start, stop = (180 - start, 180 - stop) if stop < start else (180 - stop, 180 - start)
|
start, stop = (180 - start, 180 - stop) if stop < start else (180 - stop, 180 - start)
|
||||||
if start == stop % 360:
|
if start == stop % 360:
|
||||||
self.figure.append(self.graph_elem.DrawCircle((x, y), r, fill_color=fill,
|
self.figure.append(self.graph_elem.DrawCircle((x, y), r, fill_color=fill, line_color=line, line_width=width))
|
||||||
line_color=line, line_width=width))
|
|
||||||
else:
|
else:
|
||||||
self.figure.append(self.graph_elem.DrawArc((x - r, y + r), (x + r, y - r), stop - start,
|
self.figure.append(self.graph_elem.DrawArc((x - r, y + r), (x + r, y - r), stop - start, start, style='arc', arc_color=fill))
|
||||||
start, style='arc', arc_color=fill))
|
|
||||||
|
|
||||||
def move(self, delta_x, delta_y):
|
def move(self, delta_x, delta_y):
|
||||||
"""
|
"""
|
||||||
|
@ -147,11 +145,8 @@ class Gauge():
|
||||||
dy1 = int(2 * inner_radius * math.cos(d / 180 * math.pi))
|
dy1 = int(2 * inner_radius * math.cos(d / 180 * math.pi))
|
||||||
dx2 = int(outer_radius * math.sin(d / 180 * math.pi))
|
dx2 = int(outer_radius * math.sin(d / 180 * math.pi))
|
||||||
dy2 = int(outer_radius * math.cos(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),
|
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))
|
||||||
(center_x + dx2, center_y + dy2),
|
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))
|
||||||
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):
|
def move(self, delta_x, delta_y):
|
||||||
"""
|
"""
|
||||||
|
@ -198,8 +193,7 @@ class Gauge():
|
||||||
start_y = y + start_radius * math.sin(i / 180 * math.pi)
|
start_y = y + start_radius * math.sin(i / 180 * math.pi)
|
||||||
stop_x = x + stop_radius * math.cos(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)
|
stop_y = y + stop_radius * math.sin(i / 180 * math.pi)
|
||||||
self.figure.append(self.graph_elem.DrawLine((start_x, start_y),
|
self.figure.append(self.graph_elem.DrawLine((start_x, start_y), (stop_x, stop_y), color=line_color, width=line_width))
|
||||||
(stop_x, stop_y), color=line_color, width=line_width))
|
|
||||||
|
|
||||||
def move(self, delta_x, delta_y):
|
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-')],
|
[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(),
|
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,
|
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)
|
event, values = window.read(timeout=UPDATE_FREQUENCY_MILLISECONDS)
|
||||||
if event == sg.WIN_CLOSED or event == 'Exit':
|
if event == sg.WIN_CLOSED or event == 'Exit':
|
||||||
break
|
break
|
||||||
|
if event == 'Edit Me':
|
||||||
|
sg.execute_editor(__file__)
|
||||||
window.close()
|
window.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ def main(location):
|
||||||
graph = sg.Graph(GSIZE, (0, 0), GSIZE, key='-GRAPH-', enable_events=True)
|
graph = sg.Graph(GSIZE, (0, 0), GSIZE, key='-GRAPH-', enable_events=True)
|
||||||
layout = [[graph]]
|
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
|
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,
|
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])
|
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_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)
|
event, values = window.read(timeout=UPDATE_FREQUENCY_MILLISECONDS)
|
||||||
if event == sg.WIN_CLOSED or event == 'Exit':
|
if event == sg.WIN_CLOSED or event == 'Exit':
|
||||||
break
|
break
|
||||||
if event == '-GRAPH-': # exit if clicked in the bottom left 20 x 20 pixel area
|
elif event == 'Edit Me':
|
||||||
if values['-GRAPH-'][0] < 20 and values['-GRAPH-'][1] < 20:
|
sg.execute_editor(__file__)
|
||||||
break
|
|
||||||
graph.delete_figure(rect_id)
|
graph.delete_figure(rect_id)
|
||||||
graph.delete_figure(text_id1)
|
graph.delete_figure(text_id1)
|
||||||
graph.delete_figure(text_id2)
|
graph.delete_figure(text_id2)
|
||||||
# graph.delete_figure(text_id3)
|
|
||||||
window.close()
|
window.close()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -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()
|
|
Loading…
Reference in New Issue