Merge pull request #4302 from PySimpleGUI/Dev-latest
NEW Demos - PIL Rounded Rectangle (thank you Copyright 2021 PySim…
This commit is contained in:
commit
bd20a9c837
|
@ -0,0 +1,91 @@
|
|||
from io import BytesIO
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
import PySimpleGUI as sg
|
||||
|
||||
"""
|
||||
Demo Rounded Rectangle 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 rounded_rectangle(text, font=('arial.ttf', 14), button_color=None,):
|
||||
"""
|
||||
Generate rounded image with text aligned center.
|
||||
:param text: text to show on image, '\n' to split lines.
|
||||
:type text: str
|
||||
:param font: font for text
|
||||
:type font: Tuple
|
||||
:return image in bytes
|
||||
:type: bytes
|
||||
"""
|
||||
pad, radius, spacing = 5, 10, 5
|
||||
ttf_font = ImageFont.truetype(font=font[0], size=font[1])
|
||||
if not text:
|
||||
text = ' '
|
||||
paragraph = text.split('\n')
|
||||
w = max(map(lambda x: ttf_font.getsize(x)[0], paragraph)) + 2 * pad
|
||||
h = sum(map(lambda x: ttf_font.getsize(x)[1], paragraph)) + 2 * pad + len(paragraph) * spacing
|
||||
c0, c1 = button_color if button_color else sg.theme_button_color()
|
||||
c0 = c0 if c0 != sg.COLOR_SYSTEM_DEFAULT else 'white'
|
||||
c1 = c1 if c1 != sg.COLOR_SYSTEM_DEFAULT else 'white'
|
||||
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=0, radius=radius)
|
||||
draw.multiline_text((pad, pad), text, align='center', font=ttf_font, fill=c0, spacing=spacing)
|
||||
return im_to_data(im)
|
||||
|
||||
|
||||
sg.theme("dark red")
|
||||
# sg.theme("dark green 7")
|
||||
# sg.set_options(font=("Arial", 16))
|
||||
layout = [[sg.Button('Normal Button')],
|
||||
[sg.Button('', image_data=sg.EMOJI_BASE64_HAPPY_THUMBS_UP, border_width=0, button_color=(sg.theme_background_color(), sg.theme_background_color()))],
|
||||
[sg.Button(image_data=rounded_rectangle('Button text\nwith 2 lines', font=('cour.ttf', 15)), button_color=(sg.theme_background_color(), sg.theme_background_color()), border_width=0)]]
|
||||
# [sg.Button(image_data=rounded_rectangle('Button text\nwith 2 lines', font=('cour.ttf', 15)), button_color=(sg.theme_background_color(), sg.theme_background_color()), border_width=0)]]
|
||||
|
||||
layout += [
|
||||
[sg.Button(
|
||||
image_data=rounded_rectangle(text, font=('cour.ttf', 15)),
|
||||
button_color=(sg.theme_button_color()[0], sg.theme_background_color()),
|
||||
border_width=0)]
|
||||
for text in (
|
||||
"My button",
|
||||
"Button with\n2 lines",
|
||||
"A long long ..................long line",
|
||||
"123\n2\n3")
|
||||
]
|
||||
|
||||
window = sg.Window('Image and Rounded Button', layout, finalize=True)
|
||||
|
||||
while True:
|
||||
|
||||
event, values = window.read()
|
||||
if event in (sg.WINDOW_CLOSED, "Exit"):
|
||||
break
|
||||
|
||||
window.close()
|
|
@ -0,0 +1,32 @@
|
|||
import PySimpleGUI as sg
|
||||
|
||||
"""
|
||||
Demo Desktop Widget Postit
|
||||
|
||||
Sizegrip Element is used to make a window without a titlebar be resizable.
|
||||
|
||||
There are 4 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
|
||||
|
||||
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)
|
||||
|
||||
# ----- 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
|
||||
window.read(close=True)
|
Loading…
Reference in New Issue