Skip to content

Commit 7dbdc26

Browse files
authored
Merge pull request PySimpleGUI#4316 from PySimpleGUI/Dev-latest
Dev latest
2 parents 1260bda + 6b17ef7 commit 7dbdc26

File tree

2 files changed

+89
-9
lines changed

2 files changed

+89
-9
lines changed

DemoPrograms/Demo_Program_Desktop_Widget_Postit.py renamed to DemoPrograms/Demo_Desktop_Widget_Postit.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,26 @@
55
66
Sizegrip Element is used to make a window without a titlebar be resizable.
77
8-
There are 4 lines
8+
There are 3 lines
99
1. Make the window
1010
2. Set initial size of window as the minimum
11-
3. Change the Multiline's settings to allow it to be expanded in any direction
12-
4. Read any event from the window which will close the window and return
11+
3. Read any event from the window which will close the window and return
1312
13+
Note that while the window has no scrollbar, you can still use the mousewheel to scroll
14+
1415
Copyright 2021 PySimpleGUI
1516
"""
1617

1718
# ----- Make the window -----
1819
window = sg.Window('Postit', [[sg.T('Postit Note', text_color='black', background_color='#FFFF88')],
19-
[sg.ML(size=(30, 5), background_color='#FFFF88', no_scrollbar=True, k='-ML-', border_width=0),
20-
sg.Sizegrip(background_color='#FFFF88')]], no_titlebar=True, grab_anywhere=True, margins=(0, 0), background_color='#FFFF88',
21-
element_padding=(0, 0), right_click_menu=sg.MENU_RIGHT_CLICK_EXIT, keep_on_top=True, font='_ 20', resizable=True, finalize=True)
20+
[sg.ML(size=(30, 5), background_color='#FFFF88', no_scrollbar=True, k='-ML-', border_width=0, expand_y=True, expand_x=True),
21+
sg.Sizegrip(background_color='#FFFF88')]],
22+
no_titlebar=True, grab_anywhere=True, margins=(0, 0), background_color='#FFFF88', element_padding=(0, 0),
23+
right_click_menu=sg.MENU_RIGHT_CLICK_EXIT, keep_on_top=True, font='_ 20', resizable=True, finalize=True)
2224

2325
# ----- Make sure it doesn't get any smaller than it is initially -----
2426
window.set_min_size(window.size)
2527

26-
# ----- Make the Multiline Element expandable -----
27-
window['-ML-'].expand(True, True, True)
28-
2928
# ----- Read the window and wait for any event.
3029
# ----- Any event will cause the read to return
3130
# ----- Has a right click menu that can be used to choose exit
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)