在 PySimpleGUI 中制作一个文本框

TXO*_*XOG 2 python user-interface tkinter pysimplegui

我正在关注PySimpleGUI文档并在进行过程中进行自己的编辑。我对它很陌生,并且有使用 Tkinter 的经验。Tkinter 中有一个文本框,您可以通过代码获取Text(window, width=?, height=?, wrap=WORD, background=yellow)。但是在 PySimpleGUI 中使用类似的代码: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()
Run Code Online (Sandbox Code Playgroud)

我曾尝试使用PySimpleGui:如何在文本框中输入文本?但这里的文本框实际上是一个列表框:

这个问题文本框

这与我想要的 TextBox 完全不同:

我想要的文本框

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

acw*_*668 6

您可以使用sg.Multiline(...)which 是Texttkinter 的小部件。

要获取 的内容sg.Multiline,您可以为其分配一个唯一值key,并使用它key来获取其在valuesdict 中的内容。

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

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()
Run Code Online (Sandbox Code Playgroud)