【问题标题】:Make a Textbox in PySimpleGUI在 PySimpleGUI 中创建一个文本框
【发布时间】:2020-04-15 12:14:08
【问题描述】:

我正在关注PySimpleGUI 文档并在进行过程中进行自己的编辑。我对它很陌生,并且有使用 Tkinter 的经验。 Tkinter 中有一个文本框,您可以使用代码Text(window, width=?, height=?, wrap=WORD, background=yellow) 获得。然而,在带有 similar 代码的 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()

我尝试过使用PySimpleGui: How to enter text in the text box?,但这里的文本框实际上是一个列表框:

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

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

【问题讨论】:

  • 你试过sg.Multiline.
  • 哇,它有效,这正是我需要的 @acw1668。您能否回答这个问题并展示如何从中获取变量的示例代码?从教程来看,好像整个窗口都被读取了,那么我应该如何从不同的区域中绘制不同的变量呢?
  • 对不起,我不明白你想要什么?
  • @acw1668 在教程中你会得到一个代码event, values = window.read() 的输入。当您的窗口中有多个输入时,您如何将它们分开?
  • 您可以为每个小部件分配一个key,并使用此键来识别您想要的数据。比如sg.Multiline(..., key='textbox'),那么可以使用values['textbox']获取sg.Multiline的内容。

标签: python user-interface tkinter pysimplegui


【解决方案1】:

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

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

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

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()

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 2020-07-13
    相关资源
    最近更新 更多