0

In the past, to build a radio button with Matplotlib Widgets and print the pushed button name to the terminal I have done this:

import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons

def update(val):
    print(rb.value_selected)
    fig.canvas.draw_idle()

fig, ax = plt.subplots(1, 1)
ax = plt.axes([0.5, 0.4, 0.1, 0.15], facecolor='gray')
rb = RadioButtons(ax,  ('pi', '42'), active=0)
rb.on_clicked(update)
plt.show()

Matplotlib Widget RadioButtons

The changing something on the radio button always generates an event.

Looking at the example in this answer it seems I need to also include an extra Read button; the visible appearance of the radio buttons can be different than what my script thinks the user wants until the user presses Read. Manipulating the radio buttons does not seem to generate an event. You have to then push a second button that says Hey! I've made up my mind, now take a look!

PySimpleGUI Radio Buttons

import PySimpleGUI as sg

layout = [[sg.Radio('pi', 'num', default=True) ,
           sg.Radio('42', 'num')],
          [sg.Button('Read')]]

window = sg.Window('Radio Button Example', layout)

while True:             # Event Loop
    event, values = window.Read()
    if event in (None, 'Cancel'):
        break
    print(event, values)

window.close()

This comment says

I think you've hit either a bug or the Radio Buttons enable_events isn't implemented. I thought it was but may not be on Qt. I'll make it a priority and look at the code.

which makes me think there ought to be a way to generate an event in PySimpleGUI when a radio button is changed without need for a separate button, but I can't figure out if there is one.

Question: Is there a way for PySimpleGUI Radio Buttons to generate events when changed?

uhoh
  • 3,713
  • 6
  • 42
  • 95
  • 1
    The documentation clearly discusses this. It would also be nice to post them on the GitHub as it removes one more location to check. – Mike from PSG Mar 25 '20 at 13:51
  • There it is indeed! I suppose I was overwhelmed by the 1 MB wall of text on a single page of documentation, and I wasn't expecting it to be a separate parameter for each element of a given radio button cluster, but I can work with that. – uhoh Mar 25 '20 at 15:37
  • The answer to being intimidated by the documentation is to search it (press control F), or spend the time reading it so that it's not overwhelming. The honest complaint is too much documentation? There is a table of contents along the left edge of the page. Every element has a detailed call signature for it and it's methods that shows each parameter, the type and description. At least go to that section at the end of the document. – Mike from PSG Mar 26 '20 at 12:47
  • @MikeyB Okay you're enamored with the documentation's layout, got it! – uhoh Mar 26 '20 at 13:02

1 Answers1

3

Generating events when something changes is specified by the enable_events parameter. Documented here : https://pysimplegui.readthedocs.io/en/latest/#radio-element

Example to try. Works for PySimpleGUIQt too.

import PySimpleGUI as sg

layout = [  [sg.Text('Radio Button Events')],
            [sg.Radio('1', 1, enable_events=True, key='R1'), sg.Radio('2',1, enable_events=True, key='R2')],
            [sg.Button('Go'), sg.Button('Exit')]  ]

window = sg.Window('Window Title', layout)

while True:             # Event Loop
    event, values = window.read()
    print(event, values)
    if event in (None, 'Exit'):
        break
window.close()

Mike from PSG
  • 5,312
  • 21
  • 39
  • Okay so each button (element) of a given radio button group can have events enabled or disabled individually. That's weird but okay. *Thanks!* – uhoh Mar 25 '20 at 15:39
  • Nothing weird about it. Parameter to enable events is common to all elements. It's one of 6 or 7 that nearly all elements have. https://pysimplegui.readthedocs.io/en/latest/#common-element-parameters. Happy to answer questions in the future, just not going to be doing it here. The answers you seek are likely in one of the resources provided to you if you can get over your fear of reading then you can likely answer them yourself quicker than posting. – Mike from PSG Mar 26 '20 at 12:54
  • I think it's weird to have a group of radio buttons where some generate events and others don't. My only examples before this were the actual buttons on radios from where the term originates, Matplotlib's RadioButton widget, and every UI I've ever used as an end user. So it's *de facto* weird to me. I'll look at some other examples now to see how common mixed buttons (some generating events, some not) is. – uhoh Mar 26 '20 at 13:08
  • I've just asked [Am I wrong and is it in fact not weird for some elements in a radio button group to not generate events?](https://ux.stackexchange.com/q/132274/73305) – uhoh Mar 26 '20 at 13:59
  • Your response to help is to make posts where you want to be labelled as "right"? To prove a point rather than just taking the help and moving forward with your project? Here's a GUI that sets individual radio button events... tkinter. Look up what the "command" parameter does, that is set on every Radiobutton Widget you create. If you don't like PySimpleGUI or find GUI programming weird, just move on. I hope your project goes well. Maybe less time on being right and more on learning the packages? – Mike from PSG Mar 26 '20 at 14:37
  • Cheer up, I'm always happy to be wrong, you've go the polarity backwards; every time we find out we're wrong we learn something. *I love* being wrong, it's gold! – uhoh Mar 26 '20 at 16:06