PySimpleGUI creating Text with for loop

Question:

I want to display some text using for loop and PySimpleGUI.
I have a code…

import PySimpleGUI as sg

teams = ["a","b","c","d"]
layout =  [

        [sg.Text(teams[i]), sg.Radio('Pot A', "RADIO1", default=True),
        sg.Radio('Pot B', "RADIO1")],

]

window = sg.Window('hey').Layout(layout)
button, values = window.Read()

And because i would like to avoid hardcoding I want to use for loop to create more of text for me.
example of loop…

for i in range(len(teams)):
   #create some text

I dont know where to insert my loop to make it work or if it is possible at all.

Thanks in advance!

Asked By: RandyMarsh

||

Answers:

Not sure exactly how you’re wanting to display your list of teams.

You’ve got a number of choices of how to do it regardless. One of the easiest is list comprehensions.

import PySimpleGUI as sg

teams = ["a","b","c","d"]
layout =  [[sg.Text(team) , sg.Radio('Pot A', "RADIO1"+team, default=True),
        sg.Radio('Pot B', "RADIO1"+team)] for team in teams]

window = sg.Window('hey', layout)
button, values = window.read()

Creating layouts using loops is described in the PySimpleGUI documentation:
https://pysimplegui.readthedocs.io/en/latest/#generated-layouts-for-sure-want-to-read-if-you-have-5-repeating-elementsrows

Answered By: Mike from PSG

For newscomer, you can also create a loop in such direction:

fire = ['F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10']
col = [[sg.Text('Fire', background_color='red', size=sz)]]
        # Create several similar fire buttons in the vertical column
        for card in fire:
            col += [
                [sg.Button(card, key=card)]
            ]
Answered By: Sergo