我正在使用 PySimpleGUI 库,并尝试制作一个 GUI(下面的代码)来输入一个代码字谜题(基本上与填字游戏的格式相同)。我希望我的 GUI 是一组指定尺寸的文本框,可以采用数字或字母。
它构建了一个正确格式的 GUI(built GUI),但是当我在每个框中输入数字 1-9(填充 GUI)并单击“确定”时,打印到控制台的输出为:“7,8,9, ,,,,,,",所以我假设它只是读取最后一组输入。如果我将最后一行留空并像以前一样填充前两行,则会将“,,,,,,,,,”输出到控制台。我尝试将列表理解更改为 for 循环并得到相同的结果,但是当我硬编码布局(下面的代码)并输入 1-9 时,我得到了所需的“1,2,3,4,5,6,7 ,8,9”。如何使用变量为 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)