Updated Radio Simulation using Image Elements

This commit is contained in:
PySimpleGUI 2021-10-23 08:27:49 -04:00
parent ac5ea72e13
commit d79b91c879
1 changed files with 73 additions and 21 deletions

View File

@ -1,32 +1,84 @@
import PySimpleGUI as sg import PySimpleGUI as sg
""" """
This demo uses buttons to simulate radio buttons. Demo - Simulated Radio Buttons
In this demo the variable active_radio_button has the key for the currently active button This demo shows 2 ways to achieve a radio button type of interface.
1. Uses Buttons and changes the color of the button to show which is selected
2. Uses an Image Element and a Text Element together
Copyright 2020 PySimpleGUI.org Copyright 2020, 2021 PySimpleGUI
""" """
radio_keys = ['Play', 'Stop', 'Pause', 'Off'] def using_buttons():
selected_color = ('red', 'white') radio_keys = ['Play', 'Stop', 'Pause', 'Off']
active_radio_button = None selected_color = ('red', 'white')
active_radio_button = None
layout = [ [sg.Text('My Window')], layout = [ [sg.Text('My Window')],
[sg.Text('These are simulated radio buttons')], [sg.Text('These are simulated radio buttons')],
[sg.Button(name) for name in radio_keys], [sg.Button(name) for name in radio_keys],
[sg.Button('Go'), sg.Button('Exit')] ] [sg.Button('Go'), sg.Button('Exit')] ]
window = sg.Window('Window Title', layout, use_default_focus=False) window = sg.Window('Window Title', layout, use_default_focus=False)
while True: # Event Loop while True: # Event Loop
event, values = window.read() event, values = window.read()
if event in (None, 'Exit'): if event in (None, 'Exit'):
break break
if event in radio_keys: if event in radio_keys:
for k in radio_keys:
window[k].update(button_color=sg.theme_button_color())
window[event].update(button_color=selected_color)
active_radio_button = event
window.close()
def using_images():
radio_keys = ('-R1-', '-R2-', '-R3-')
def check_radio(key):
for k in radio_keys: for k in radio_keys:
window[k].update(button_color=sg.theme_button_color()) window[k].update(radio_unchecked)
window[event].update(button_color=selected_color) window[k].metadata = False
active_radio_button = event window[key].update(radio_checked)
window[key].metadata = True
def radio_is_checked(key):
return window[key].metadata
layout = [[sg.T('Radio Buttons Custom')],
[sg.Image(radio_checked, enable_events=True, k='-R1-', metadata=True), sg.T('Radio Button 1', enable_events=True, k='-T1-')],
[sg.Image(radio_unchecked, enable_events=True, k='-R2-', metadata=False), sg.T('Radio Button 2', enable_events=True, k='-T2-')],
[sg.Image(radio_unchecked, enable_events=True, k='-R3-', metadata=False), sg.T('Radio Button 3', enable_events=True, k='-T3-')],
[sg.Exit()]]
window = sg.Window('Radio Buttons Simulated Using Image Element', layout)
while True:
event, values = window.read()
print(event, values)
if event in (sg.WIN_CLOSED, 'Exit'):
break
if event in radio_keys:
check_radio(event)
elif event.startswith('-T'): # If text element clicked, change it into a radio button key
check_radio(event.replace('T', 'R'))
for k in radio_keys:
print(f'Radio key {k} is {radio_is_checked(k)}')
window.close()
if __name__ == '__main__':
# Base64 Encoded Radio Button Image of unchecked radio button
radio_unchecked = b'iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAEwElEQVR4nI1W3W9URRT/nZm7ZXdpbajdWpCAjcFEqw88+CACrgaBmFBIwI3fPPpPaJYND/wjYsxFYgwP+BV2kY9gNCIJIhEIBZSWLl3aprvde2fOOT7c3W27fNSTTO7cMzO/35wz55wZYAVRVVMuaxCGoV2qD8PQlsvlQFXNShhPAqduYEr0lrrmhmFoVbVbvWzdQxKGoS0UCgwAFy6PvySx27cQRVvY80YGZyHaIKJbPUHqvCF8k3/tlb+61z2RJAzVFgrE5QuX1q9K9x6Oouj9TCazKmUBawiAglkQO0bsPOqNejOw9qsoan62Z8eWfx9FRMsJkgnnfrv6FgXBUWOD4UzAWJsb8L3ZNFlrCQSwZ8TO6excXe/eux/UY0EcuQkXRx/t3fX6qW6iDomqGiKS87///QaM/Q7K6efXD7rBgf5AVcl7hgBQEYgqVAQEgqroZLXmb9yeTLGgKRztHtu5/XQbr0NSVDU4dAhvj703LGouBpaGXhwZ5v6nem0cO2gCB002AxGBiICZwSwIrEVtZpav3LhjneN76YxsvnDq1D0AKJVKYgBg9NgxKpVKIkpH0ulVQyPrBvxTfb02ih2ICESAdp2darJHIkIUx+jrXW03rB30PT09zzTm5UipVJLR0VECAGqb9csfV16oN3H56f60Hd20gZzzRJR4UzvAusySxBoBi8A5DyLolWvjOv1gjldnUqN7duavFYtFYyoVGACIvd2fzWZSw4P9IqKkLfBugu4GKFSSr4hSbqBfMplMaiFyBwAgn88bU60eUwCI43hbYIBsJk2e+bHAiQVL/xWiSTB4ZmQzabKG4B1vBYBqtapBoVBgVaUfz13aaI3CEBGzgAjouEuXg3bARSG6pImADJEhwLN/TlWJiDhoecOqSHYpUIJPHYclY4CqdBElZ6Otfse9otlKBRaAb5OwqjbaYSnatqKzpEXQAleFsIAlCWERBbfyR4TBwlDVRj4PBgAThqElIgVhPPaicew02R0vi6ClESWcALEkkbV0bhQ7dZ4VpONEpGEYWpPL5QgArLVnYsc0N99QAuC5nWy8JPEYvtW4PS6LfVXFfL2hznkyxv4MALlcjkwlnxcACCj4ul6fjyeqNeOZ1Xu/COoXwX0XkbDAs8B7BjPrVLVm6vVGDOXjAFCpVMSUiCQMQ/vmlpevE+nRyJOZul9jYwix84sEfrG1d94h9A5EQHW6xrEXYwhffFLYe/3dMLSlUkmS2lUsGgB4Nf/OEIleJEPDI88Ocl/vauu8b5UQdA69nS/t2mWIMDM3x+P/TFp2flKM3Tz+569T7dr1UBU+8dPZbWRS30M4s25ojVvT3xcIlNpRpCpd+cI6XZvxd6emUyrUEPW7DhbGzi6twp37mVpu27Nj65lmo7lbgDsT9+dSV2/cotqDWR/HMYt4ERHx7CWKIq7NzPrrN2/TVG0uBcVt56PdBwtjZ1sRKx3sruLaubiOnzy51tq+wy6KP0j19GSsAQwtlnrPjNgxmgvNBWvNl41m8/NPP94/seLN2E0EACd+qGxyse5runi7Zz+iLL2imLcGN1PWnhYNvv3wwM5r3ev+lzzqtdLSB926lV4rK0qxWDTlcvmx7652ZD5J/gNoDCDS80MCGwAAAABJRU5ErkJggg=='
# Base64 Encoded Radio Button Image of checked radio button
radio_checked = b'iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAF40lEQVR4nI2Wf2yWVxXH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