Add a rounded and squared togglebutton
See original GitHub issue@daniilS, the toggle style you added gave me the ability to create a rounded toggle switch. I’m modeling it after the one here. I’d like to have rounded and squared with images generated in the application at run-time so that I don’t have to package the assets with the program. This also gives the ability to generate hover effects by dimming the color slightly with the update_hsv
method.
The approach I took was to draw a rounded rectangle very large, and then shrink it to the size I need, apply anti-aliasing to get a nice smooth look. I’m not completely happy with the size or dimensions. But, it is getting close.
Let me know what you think if you have time to play around with it; and of course if you have any feedback.
from PIL import Image, ImageTk, ImageDraw
from tkinter import ttk
on_color='#2196F3'
off_color = '#cccccc'
# off image
off_im = Image.new('RGBA', (226, 130))
draw = ImageDraw.Draw(off_im)
draw.rounded_rectangle([1, 1, 225, 129], radius=(128/2), fill=off_color)
draw.ellipse([14, 14, 114, 114], fill='white')
# on image
on_im = Image.new('RGBA', (226, 130))
draw = ImageDraw.Draw(on_im)
draw.rounded_rectangle([1, 1, 225, 129], radius=(128/2), fill=on_color)
draw.ellipse([12, 12, 116, 116], fill='white')
on_im = on_im.transpose(Image.ROTATE_180)
# set up application styles
style = ttk.Style()
style.theme_use('clam')
# set the background white
style.configure('white.TFrame', background='white')
window = ttk.Frame(style.master, style='white.TFrame', padding=20)
window.pack(fill='both', expand='yes')
# save reference to images
toggle_off = ImageTk.PhotoImage(off_im.resize((32, 20), Image.LANCZOS))
toggle_on = ImageTk.PhotoImage(on_im.resize((32, 20), Image.LANCZOS))
# create a new image element
style.element_create('rounded.Toolbutton.label', 'image', toggle_on, ('!selected', toggle_off))
# set the new layout
style.layout('rounded.Toolbutton', [
('Toolbutton.border', {'sticky': 'nswe', 'children': [
('Toolbutton.padding', {'sticky': 'nswe', 'children': [
('rounded.Toolbutton.label', {'sticky': 'nswe'})]})]})])
# configure style settings
style.configure('rounded.Toolbutton', relief='flat', borderwidth=0)
style.map('rounded.Toolbutton',
background=[
('selected', 'white'),
('!selected', 'white')])
# demo the new widget
ttk.Checkbutton(window, style='rounded.Toolbutton').pack()
window.mainloop()
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (13 by maintainers)
Top Results From Across the Web
Rounded corner for ToggleButton in UWP - Stack Overflow
To get a toggle button with rounded corners you can do the following : Right click on the button and select Edit Template...
Read more >ToggleButton.Shape property (Access) - Microsoft Learn
The Shape property uses the following settings. Setting, Description. 0, Rectangle. 1 (Default), Rounded Rectangle. 2, Snip Single Corner ...
Read more >Flutter Toggle Buttons in Multiple Rows with Spacing and ...
A ToggleButtons widget is arranged in a row without spacing and rounded corners can only be applied to the first and last buttons....
Read more >WPF Controls | 30-ToggleButton | Part 2 - YouTube
Welcome to WPF tutorials | Toggle Button in WPFIn this part of our Toggle Button Control, we're going to focus on customization ....
Read more >Custom Button - Rounded, Pill or Square Shape - WinForm C#
[ Custom controls in Windows Forms and C# ] ⏮ PREVIOUS: Toggle Button or Switch Button https://youtu.be/m7Iv6xfjnuw ⏭ NEXT: Custom ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
this almost makes me want to draw my own radio and check buttons…
light theme color variations for
Roundtoggle.Toolbutton
dark theme color variations for
Roundtoggle.Toolbutton