From e68d3005d183293b7373dfe50d5c7dfbebcb5d49 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Mon, 24 May 2021 10:08:06 -0400 Subject: [PATCH 1/2] Renamed Postit demo to match others. Use the new multiline expand_x, expand_y. --- ..._Postit.py => Demo_Desktop_Widget_Postit.py} | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) rename DemoPrograms/{Demo_Program_Desktop_Widget_Postit.py => Demo_Desktop_Widget_Postit.py} (59%) diff --git a/DemoPrograms/Demo_Program_Desktop_Widget_Postit.py b/DemoPrograms/Demo_Desktop_Widget_Postit.py similarity index 59% rename from DemoPrograms/Demo_Program_Desktop_Widget_Postit.py rename to DemoPrograms/Demo_Desktop_Widget_Postit.py index c697a7f0..c4e4e889 100644 --- a/DemoPrograms/Demo_Program_Desktop_Widget_Postit.py +++ b/DemoPrograms/Demo_Desktop_Widget_Postit.py @@ -5,27 +5,26 @@ import PySimpleGUI as sg Sizegrip Element is used to make a window without a titlebar be resizable. - There are 4 lines + There are 3 lines 1. Make the window 2. Set initial size of window as the minimum - 3. Change the Multiline's settings to allow it to be expanded in any direction - 4. Read any event from the window which will close the window and return + 3. Read any event from the window which will close the window and return + Note that while the window has no scrollbar, you can still use the mousewheel to scroll + Copyright 2021 PySimpleGUI """ # ----- Make the window ----- window = sg.Window('Postit', [[sg.T('Postit Note', text_color='black', background_color='#FFFF88')], - [sg.ML(size=(30, 5), background_color='#FFFF88', no_scrollbar=True, k='-ML-', border_width=0), - sg.Sizegrip(background_color='#FFFF88')]], no_titlebar=True, grab_anywhere=True, margins=(0, 0), background_color='#FFFF88', - element_padding=(0, 0), right_click_menu=sg.MENU_RIGHT_CLICK_EXIT, keep_on_top=True, font='_ 20', resizable=True, finalize=True) + [sg.ML(size=(30, 5), background_color='#FFFF88', no_scrollbar=True, k='-ML-', border_width=0, expand_y=True, expand_x=True), + sg.Sizegrip(background_color='#FFFF88')]], + no_titlebar=True, grab_anywhere=True, margins=(0, 0), background_color='#FFFF88', element_padding=(0, 0), + right_click_menu=sg.MENU_RIGHT_CLICK_EXIT, keep_on_top=True, font='_ 20', resizable=True, finalize=True) # ----- Make sure it doesn't get any smaller than it is initially ----- window.set_min_size(window.size) -# ----- Make the Multiline Element expandable ----- -window['-ML-'].expand(True, True, True) - # ----- Read the window and wait for any event. # ----- Any event will cause the read to return # ----- Has a right click menu that can be used to choose exit From 6b17ef7ded98515a4b6698d63235a5d2c7a65f6c Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Mon, 24 May 2021 10:10:03 -0400 Subject: [PATCH 2/2] Toggle button demo using PIL to draw the button --- DemoPrograms/Demo_PIL_Toggle_Button.py | 81 ++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 DemoPrograms/Demo_PIL_Toggle_Button.py diff --git a/DemoPrograms/Demo_PIL_Toggle_Button.py b/DemoPrograms/Demo_PIL_Toggle_Button.py new file mode 100644 index 00000000..db16422b --- /dev/null +++ b/DemoPrograms/Demo_PIL_Toggle_Button.py @@ -0,0 +1,81 @@ +from io import BytesIO +from PIL import Image, ImageDraw, ImageFont +import PySimpleGUI as sg + +""" + Demo Toggle Buttons created using PIL + + A BIG thank you to @jason990420 for his talented work creating this demo. + + Demonstrates how you can draw buttons rather than using the built-in buttons + with the help of the PIL module. + + One advantage is that the buttons will match your window's theme. + + You'll find more PySimpleGUI programs featured in + Mike Driscoll's PIL book - "Pillow: Image Processing with Python" + + Copyright 2021 PySimpleGUI, @jason990420 +""" + + +def im_to_data(im): + """ + Convert a PIL.Image.Image to a bytes image + :param im: Image + :type: PIL.Image.Image object + :return image in bytes + :type: bytes + """ + with BytesIO() as buffer: + im.save(buffer, format='PNG') + data = buffer.getvalue() + return data + + +def toggle_button(button_color=None, size=(100, 40), on=True): + """ + + :return image in bytes + :type: bytes + """ + pad, radius, spacing = 5, 10, 5 + w, h = size + c1, c2 = button_color if button_color else (sg.theme_input_background_color(), sg.theme_background_color()) + im = Image.new("RGBA", (w, h), (255, 255, 255, 0)) + draw = ImageDraw.Draw(im) + draw.rounded_rectangle((0, 0, w - 1, h - 1), fill=c1, width=3, radius=radius) + if on: + draw.rounded_rectangle((2, 2, w - size[1], h - 3), fill=c2, width=3, radius=radius) + else: + draw.rounded_rectangle((size[1], 2, w - 2, h - 3), fill=c2, width=3, radius=radius) + + return im_to_data(im) + + + + +sg.theme("dark red") +# sg.theme("dark green 7") +# sg.theme("dark gray 13") +button_on = toggle_button() +button_off = toggle_button(on=False) +layout = [[sg.T('PIL Made Toggle Buttons')], + [sg.T('On'), sg.Button(image_data=button_on, button_color=(sg.theme_background_color(), sg.theme_background_color()), border_width=0, k='-B1-', metadata=True), sg.T('Off')], + [sg.Button(image_data=button_off, button_color=(sg.theme_background_color(), sg.theme_background_color()), border_width=0, k='-B2-', metadata=False)]] + + +window = sg.Window('PIL Buttons', layout, finalize=True, element_justification='c', use_custom_titlebar=True, font='_ 18') + +while True: + + event, values = window.read() + if event in (sg.WINDOW_CLOSED, "Exit"): + break + window[event].metadata = not window[event].metadata + if window[event].metadata: + window[event].update(image_data=button_on) + else: + window[event].update(image_data=button_off) + +window.close()