在 PySimpleGUI 中制作文本框

Make a Textbox in PySimpleGUI

我正在关注 PySimpleGUI 文档并在进行过程中进行自己的编辑。我对它很陌生并且有使用 Tkinter 的经验。 Tkinter 中有一个文本框,您可以使用代码 Text(window, width=?, height=?, wrap=WORD, background=yellow) 获得它。但是在 PySimpleGUI 中使用 similar 代码:layout = [[sg.Text('Some text on Row 1')]] - 创建一个标签。我的代码是:

import PySimpleGUI as sg

sg.theme('DarkAmber')   # Add a touch of color
# All the stuff inside your window.
layout = [  [sg.Text('Some text on Row 1')],
            [sg.Text('Enter something on Row 2'), sg.InputText()],
            [sg.Button('Ok'), sg.Button('Close Window')],
            [sg.Text('This is some text', font='Courier 12', text_color='blue', background_color='green')],
            [sg.Listbox(values=('value1', 'value2', 'value3'), size=(30, 2), key='_LISTBOX_')]]

# Create the Window
window = sg.Window('Test', layout).Finalize()
window.Maximize()
# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()
    if event in (None, 'Close Window'): # if user closes window or clicks cancel
        break
    print('You entered ', values[0])

window.close()

我曾尝试使用 ,但此处的文本框实际上是一个列表框:

这与我想要的文本框完全不同:

TextBox 被红线包围。有人可以帮我找到可以提供我想要的 TextBox 的代码吗?

您可以使用 sg.Multiline(...),它是 tkinter 的 Text 小部件。

要获取 sg.Multiline 的内容,您可以为其分配一个唯一的 key 并使用此 key 获取其在 values 字典中的内容。

以下是基于您的代码的示例:

import PySimpleGUI as sg

sg.theme('DarkAmber')   # Add a touch of color
# All the stuff inside your window.
layout = [  [sg.Text('Some text on Row 1')],
            [sg.Text('Enter something on Row 2'), sg.InputText()],
            [sg.Button('Ok'), sg.Button('Close Window')],
            [sg.Multiline(size=(30, 5), key='textbox')]]  # identify the multiline via key option

# Create the Window
window = sg.Window('Test', layout).Finalize()
#window.Maximize()
# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()
    if event in (None, 'Close Window'): # if user closes window or clicks cancel
        break
    print('You entered in the textbox:')
    print(values['textbox'])  # get the content of multiline via its unique key

window.close()