Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create radio buttons from a list using PySimpleGui?

Tags:

pysimplegui

I want use PySimpleGui to dynamically create radio buttons from a list, but my efforts to insert a loop in the layout code are catching syntax errors. Can this be done with the API or do I need to slog it out with tkinter? My list is being generated by a targeted file search of a network drive.

I've tried concatenating 'layout', putting the radio button section in a for loop. Also attempted to insert a for loop in the [sg.Radio()] declaration itself. Neither works.

import PySimpleGUI as sg

xList = ['a', 'b', ... 'zz']

layout = [[sg.Text('Select a thingy')],
          [sg.Radio(<for thingy in xList: 'thingy', thingy>)],
                   #^^^^^^ for loop is psuedo code
          [sg.OK(), sg.Cancel()]]
like image 370
OldFartN00B Avatar asked May 22 '19 16:05

OldFartN00B


People also ask

How do you add radio buttons to groups?

You group radio buttons by drawing them inside a container such as a Panel control, a GroupBox control, or a form. All radio buttons that are added directly to a form become one group. To add separate groups, you must place them inside panels or group boxes.

Can we select multiple radio buttons?

Only one radio button in a group can be selected at the same time. Note: The radio group must have share the same name (the value of the name attribute) to be treated as a group.


1 Answers

I think this is what you're looking for?

import PySimpleGUI as sg

radio_choices = ['a', 'b', 'c']
layout = [
            [sg.Text('My layout')],
            [sg.Radio(text, 1) for text in radio_choices],
            [sg.Button('Read')]
         ]

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

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

It produces this window:

enter image description here

There are a number of ways of "building" a layout variable. Here are a couple of other combinations that produce the same window:

This first one builds one row at a time and then adds them together in the end

# Build Layout
top_part = [[sg.Text('My layout')]]
radio_buttons = [[sg.Radio(x,1) for x in radio_choices]]
read = [[sg.Button('Read')]]
layout = top_part + radio_buttons + read

This one also builds a single row at a time and then adds them together, but it does it in a single statement instead of 4.

   # Build layout
    layout = [[sg.Text('My layout')]] + \
                [[sg.Radio(text, 1) for text in radio_choices]] + \
                [[sg.Button('Read')]]

If you wanted to add these buttons one per line, then there are several ways of doing this too. If you are using Python 3.6, then this will work:

layout = [
            [sg.Text('My layout')],
            *[[sg.Radio(text, 1),] for text in radio_choices],
            [sg.Button('Read')]
         ]

The "Build a layout" technique will work on systems where the above * operator is not valid.

radio_choices = ['a', 'b', 'c']
radio = [[sg.Radio(text, 1),] for text in radio_choices]
layout = [[sg.Text('My layout')]] + radio + [[sg.OK()]]

Both of these variations when combined with the window code and the event loop, will produce a window that looks like this: enter image description here

like image 80
Mike from PSG Avatar answered Sep 24 '22 09:09

Mike from PSG