问题
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()]]
回答1:
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:
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:
来源:https://stackoverflow.com/questions/56261629/how-can-i-create-radio-buttons-from-a-list-using-pysimplegui