Reputation: 23
I'm trying to capture text box input as a tuple, and pass it along to a for
loop that will perform an action for each item in the tuple. Ideally, this tuple would be dynamically sized, i.e. one iteration of the program might pass the tuple as ("1", "2", "3") and another might be ("1"). Then, in the code below, I'm trying to print each item of the tuple.
import PySimpleGUI as sg
layout = [[sg.InputText('',key = '-IN-'), sg.Button("OK")]]
# Create the window
window = sg.Window("Window Name", layout, margins=(256, 144))
# Create an event loop
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event =="Cancel":
break
name = [values['-IN-']]
listLength = len(name)
# What I would like to do:
for i in range(listLength):
print(name[i])
I haven't found good documentation for this yet. All I know is that keys "can be ANYTHING (ints, tuples, objects). Anything EXCEPT Lists. Lists are not valid Keys because in Python lists are not hashable and thus cannot be used as keys in dictionaries. Tuples, however, can." (PysimpleGUI cookbook).
The key '-IN-'
needs to be changed to something. I think the key needs to be assigned to a tuple, but I haven't seen any examples of what that syntax looks like. Is there any documentation I can reference for this? What should the key look like to become a tuple? I would prefer to pass the text box input as an array, but I haven't seen any information about key arrays yet.
Upvotes: 0
Views: 361
Reputation: 13061
There's nothing special, just using tuple as key of element.
For example, if you create an input table, by using tuple key, like (row, column)
, it will be much easy to refer each input element, something like array index.
You can find tuple as the value of option key
of element, also access the dictionary values
by tuple key.
import PySimpleGUI as sg
def main():
layout = [
[sg.Input(size=(10, 1), key=(i, j)) for j in range(3)] for i in range(5)
] + [
[sg.Button('OK')]
]
window = sg.Window("Title", layout, finalize=True)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == 'OK':
data = [[values[(i, j)] for j in range(3)] for i in range(5)]
print(data)
window.close()
if __name__ == '__main__':
main()
After OK
button clicked
[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['10', '11', '12'], ['13', '14', '15']]
Upvotes: 1