Reputation: 262
I'm using the PySimpleGUI library, and trying to make a GUI (code below) to input a codeword puzzle (essentially the same format as a crossword). I want my GUI to be an array of textboxes of specified dimensions that can take a number or letter.
It builds a GUI of the correct format (built GUI), but when I enter the numbers 1-9 in each box (filled GUI) and click "OK", the output printed to the console is: "7,8,9,,,,,,,", so I assumed it is only reading the last set of inputs. If I leave the last row blank and fill the top two rows as before, I get ",,,,,,,,," outputted to the console. I tried changing the list comprehensions to for loops and got the same result, but when I hardcoded the layout (code below) and entered in 1-9, I got the desired "1,2,3,4,5,6,7,8,9,". How do you implement a layout for a PySimpleGUI using a variable(s)?
# original code
import PySimpleGUI as sg
def entryGUI(length, width):
line = [sg.InputText('', size=(3, 1)) for i in range(length)]
entryLayout = [line for i in range(width)]
entryLayout.append([sg.CloseButton("OK"), sg.CloseButton("Cancel")])
entryWin = sg.Window("CodeWord Solver").Layout(entryLayout)
button, values = entryWin.Read()
for value in values:
print(value + ",", end="")
entryGUI(3, 3)
# hardcoded code
import PySimpleGUI as sg
def entryGUI(length, width):
entryLayout = [
[sg.InputText('', size=(3, 1)), sg.InputText('', size=(3, 1)), sg.InputText('', size=(3, 1))],
[sg.InputText('', size=(3, 1)), sg.InputText('', size=(3, 1)), sg.InputText('', size=(3, 1))],
[sg.InputText('', size=(3, 1)), sg.InputText('', size=(3, 1)), sg.InputText('', size=(3, 1))],
[sg.CloseButton("OK"), sg.CloseButton("Cancel")]
]
entryWin = sg.Window("CodeWord Solver").Layout(entryLayout)
button, values = entryWin.Read()
# if button != "OK":
# exit()
# else:
for value in values:
print(value + ",", end="")
#return values
entryGUI(3, 3)
Upvotes: 2
Views: 4124
Reputation: 5754
The problem you're having is that you created a single variable called line
that was a list of 3 objects. Elements are individual objects. The code instantiated 3 InputText Elements and stacked them on top of each other. This means you are working with the exact same 3 input elements on each row.
A way around this is to create 9 InputText elements, just like you did manually, but instead built inside of a loop.
Try this code:
import PySimpleGUI as sg
def entryGUI(length, width):
entryLayout = []
for i in range(width):
line = [sg.InputText('', size=(3, 1)) for i in range(length)]
entryLayout.append(line)
entryLayout.append([sg.CloseButton("OK"), sg.CloseButton("Cancel")])
entryWin = sg.Window("CodeWord Solver").Layout(entryLayout)
button, values = entryWin.Read()
print (values)
for value in values:
print(value + ",", end="")
entryGUI(3, 3)
Or for a shorter and even simpler one:
import PySimpleGUI as sg
def entryGUI(length, width):
entryLayout = [[sg.InputText('', size=(3, 1)) for i in range(length)] for _ in range(width)] + [[sg.CloseButton("OK"), sg.CloseButton("Cancel")]]
entryWin = sg.Window("CodeWord Solver").Layout(entryLayout)
button, values = entryWin.Read()
print(','.join(values))
entryGUI(3, 3)
Upvotes: 1