|
| 1 | +from io import BytesIO |
| 2 | +from PIL import Image, ImageDraw, ImageFont |
| 3 | +import PySimpleGUI as sg |
| 4 | + |
| 5 | +""" |
| 6 | + Demo Toggle Buttons created using PIL |
| 7 | +
|
| 8 | + A BIG thank you to @jason990420 for his talented work creating this demo. |
| 9 | +
|
| 10 | + Demonstrates how you can draw buttons rather than using the built-in buttons |
| 11 | + with the help of the PIL module. |
| 12 | +
|
| 13 | + One advantage is that the buttons will match your window's theme. |
| 14 | +
|
| 15 | + You'll find more PySimpleGUI programs featured in |
| 16 | + Mike Driscoll's PIL book - "Pillow: Image Processing with Python" |
| 17 | +
|
| 18 | + Copyright 2021 PySimpleGUI, @jason990420 |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +def im_to_data(im): |
| 23 | + """ |
| 24 | + Convert a PIL.Image.Image to a bytes image |
| 25 | + :param im: Image |
| 26 | + :type: PIL.Image.Image object |
| 27 | + :return image in bytes |
| 28 | + :type: bytes |
| 29 | + """ |
| 30 | + with BytesIO() as buffer: |
| 31 | + im.save(buffer, format='PNG') |
| 32 | + data = buffer.getvalue() |
| 33 | + return data |
| 34 | + |
| 35 | + |
| 36 | +def toggle_button(button_color=None, size=(100, 40), on=True): |
| 37 | + """ |
| 38 | +
|
| 39 | + :return image in bytes |
| 40 | + :type: bytes |
| 41 | + """ |
| 42 | + pad, radius, spacing = 5, 10, 5 |
| 43 | + w, h = size |
| 44 | + c1, c2 = button_color if button_color else (sg.theme_input_background_color(), sg.theme_background_color()) |
| 45 | + im = Image.new("RGBA", (w, h), (255, 255, 255, 0)) |
| 46 | + draw = ImageDraw.Draw(im) |
| 47 | + draw.rounded_rectangle((0, 0, w - 1, h - 1), fill=c1, width=3, radius=radius) |
| 48 | + if on: |
| 49 | + draw.rounded_rectangle((2, 2, w - size[1], h - 3), fill=c2, width=3, radius=radius) |
| 50 | + else: |
| 51 | + draw.rounded_rectangle((size[1], 2, w - 2, h - 3), fill=c2, width=3, radius=radius) |
| 52 | + |
| 53 | + return im_to_data(im) |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +sg.theme("dark red") |
| 59 | +# sg.theme("dark green 7") |
| 60 | +# sg.theme("dark gray 13") |
| 61 | +button_on = toggle_button() |
| 62 | +button_off = toggle_button(on=False) |
| 63 | +layout = [[sg.T('PIL Made Toggle Buttons')], |
| 64 | + [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')], |
| 65 | + [sg.Button(image_data=button_off, button_color=(sg.theme_background_color(), sg.theme_background_color()), border_width=0, k='-B2-', metadata=False)]] |
| 66 | + |
| 67 | + |
| 68 | +window = sg.Window('PIL Buttons', layout, finalize=True, element_justification='c', use_custom_titlebar=True, font='_ 18') |
| 69 | + |
| 70 | +while True: |
| 71 | + |
| 72 | + event, values = window.read() |
| 73 | + if event in (sg.WINDOW_CLOSED, "Exit"): |
| 74 | + break |
| 75 | + window[event].metadata = not window[event].metadata |
| 76 | + if window[event].metadata: |
| 77 | + window[event].update(image_data=button_on) |
| 78 | + else: |
| 79 | + window[event].update(image_data=button_off) |
| 80 | + |
| 81 | +window.close() |
0 commit comments