[英]Read() method for InputText in PySimpleGUI only reading last row with variable layout
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).我正在使用 PySimpleGUI 库,并尝试制作一个 GUI(下面的代码)来输入一个码字谜题(基本上与填字游戏的格式相同)。 I want my GUI to be an array of textboxes of specified dimensions that can take a number or letter.
我希望我的 GUI 是一个可以接受数字或字母的指定尺寸的文本框数组。
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.它构建了一个正确格式的GUI (内置 GUI ),但是当我在每个框中输入数字 1-9(填充的 GUI )并单击“确定”时,打印到控制台的输出是:“7,8,9, ,,,,,,",所以我假设它只读取最后一组输入。 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,".
我尝试将列表推导式更改为 for 循环并得到相同的结果,但是当我对布局(下面的代码)进行硬编码并输入 1-9 时,我得到了所需的“1,2,3,4,5,6,7 ,8,9,”。 How do you implement a layout for a PySimpleGUI using a variable(s)?
如何使用变量实现 PySimpleGUI 的布局?
# 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)
The problem you're having is that you created a single variable called line
that was a list of 3 objects.您遇到的问题是您创建了一个名为
line
变量,它是一个包含 3 个对象的列表。 Elements are individual objects.元素是单独的对象。 The code instantiated 3 InputText Elements and stacked them on top of each other.
该代码实例化了 3 个 InputText 元素并将它们堆叠在一起。 This means you are working with the exact same 3 input elements on each row.
这意味着您在每行上使用完全相同的 3 个输入元素。
A way around this is to create 9 InputText elements, just like you did manually, but instead built inside of a loop.解决此问题的一种方法是创建 9 个 InputText 元素,就像您手动执行的那样,而是在循环内部构建。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.