diff --git a/DemoPrograms/Demo_Desktop_Widget_RAM_Used.py b/DemoPrograms/Demo_Desktop_Widget_RAM_Used.py new file mode 100644 index 00000000..07361966 --- /dev/null +++ b/DemoPrograms/Demo_Desktop_Widget_RAM_Used.py @@ -0,0 +1,53 @@ +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() diff --git a/DemoPrograms/Demo_Graph_Elem_Image_Album.py b/DemoPrograms/Demo_Graph_Elem_Image_Album.py index aadabd38..b4d1c5a9 100644 --- a/DemoPrograms/Demo_Graph_Elem_Image_Album.py +++ b/DemoPrograms/Demo_Graph_Elem_Image_Album.py @@ -5,6 +5,7 @@ import PIL import io import base64 import os +from typing import Union, Tuple """ Demo Image Album.... displays images on Graph Element and transitions @@ -18,27 +19,22 @@ import os G_SIZE = (800,600) -def get_img_filename(f, resize=None): - """ - Resizes an image file and returns the result as a byte string which can be used to update a PySimpleGUI Element - """ - img = PIL.Image.open(f) - return resize_pil_image(img, resize) +def convert_to_bytes(file_or_bytes, resize=None): + ''' + Will convert into bytes and optionally resize an image that is a file or a base64 bytes object. + :param file_or_bytes: either a string filename or a bytes base64 image object + :type file_or_bytes: (Union[str, bytes]) + :param resize: optional new size + :type resize: (Tuple[int, int] or None) + :return: (bytes) a byte-string object + :rtype: (bytes) + ''' + if isinstance(file_or_bytes, str): + img = PIL.Image.open(file_or_bytes) + else: + img = PIL.Image.open(io.BytesIO(base64.b64decode(file_or_bytes))) -def get_img_data(data, resize=None): - """Generate PIL.Image data using PIL - """ - img = PIL.Image.open(io.BytesIO(base64.b64decode(data))) - return resize_pil_image(img, resize) - -def resize_pil_image(img, resize=None): - """ - Resizes a PIL image to a new size or just converts to PNG as a byte string - :param img: - :param resize: - :return: - """ cur_width, cur_height = img.size if resize: new_width, new_height = resize @@ -63,10 +59,11 @@ layout = [[graph]] window = sg.Window('Scrolling Image Viewer', layout, margins=(0,0), element_padding=(0,0), use_default_focus=False, finalize=True) +window.read() offset, move_amount, direction = 0, 5, 'left' while True: file_to_display = os.path.join(folder, fnames[offset]) - img_data = get_img_filename(file_to_display, resize=G_SIZE) + img_data = convert_to_bytes(file_to_display, resize=G_SIZE) id = graph.draw_image(data=img_data, location=(0, G_SIZE[1])) event, values = window.read()