Skip to content

Instantly share code, notes, and snippets.

@israel-dryer
Created May 19, 2021 12:36
Show Gist options
  • Save israel-dryer/5ec589c0711ef58dc05626fa06474a65 to your computer and use it in GitHub Desktop.
Save israel-dryer/5ec589c0711ef58dc05626fa06474a65 to your computer and use it in GitHub Desktop.
Create a custom sizegrip style
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageDraw, ImageTk
def sizegrip_style(background, foreground):
"""Create style configuration for ttk sizegrip
Args:
background (str): The color used for the background.
foreground (str): The color used for the grips
Returns:
dict: The settings used to update the sizegrip style.
"""
image = create_sizegrip_image(foreground)
settings = dict()
settings.update({
'Sizegrip.sizegrip': {
'element create': ('image', image)},
'TSizegrip': {
'configure': {'background': background},
'layout': [('Sizegrip.sizegrip', {'side': 'bottom', 'sticky': 'se'})]}})
return settings
def create_sizegrip_image(color):
"""Create assets for size grip
Args:
color (str): The hexadecimal color to use for drawing the widget.
"""
im = Image.new('RGBA', (14, 14))
draw = ImageDraw.Draw(im)
# draw the grips
draw.rectangle((9, 3, 10, 4), fill=color) # top row
draw.rectangle((6, 6, 7, 7), fill=color) # middle row
draw.rectangle((9, 6, 10, 7), fill=color)
draw.rectangle((3, 9, 4, 10), fill=color) # bottom row
draw.rectangle((6, 9, 7, 10), fill=color)
draw.rectangle((9, 9, 10, 10), fill=color)
# could also save the image here... im.save(filepath)
return ImageTk.PhotoImage(im)
if __name__ == '__main__':
root = tk.Tk()
style = ttk.Style()
style.theme_use('default')
sg_settings = sizegrip_style(background='#fff', foreground='#000')
style.theme_settings('default', sg_settings)
sg = ttk.Sizegrip(root)
sg.pack(fill='both', expand='yes')
root.mainloop()
@israel-dryer
Copy link
Author

Np. Glad it was helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment