4

I am following the PySimpleGUI documentation and making my own edits as I go along. I am very new to it and have had experience with using Tkinter. There is a Textbox in Tkinter which you an get with the code Text(window, width=?, height=?, wrap=WORD, background=yellow). However in PySimpleGUI with similar code: layout = [[sg.Text('Some text on Row 1')]] - creates a label. My code is:

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

I have attempted using PySimpleGui: How to enter text in the text box? but the Text Box here is actually a list box:

This questions Text Box

which is nothing like the TextBox I want:

TextBox I want

The TextBox is surrounded by the red lines. Can someone please help me find the code that will give me the TextBox that I desire?

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
Thomas
  • 1,214
  • 4
  • 18
  • 45
  • Did you try [sg.Multiline](https://pysimplegui.readthedocs.io/en/latest/#multiline-element). – acw1668 Apr 15 '20 at 12:36
  • Wow it works and is what I need @acw1668 . Could you potentially answer the question and also show example code of how I can get a variable from it? From the tutorial it looks as if the whole window is read so how am I supposed to draw separate variables from separate areas from it ? – Thomas Apr 15 '20 at 18:10
  • Sorry, I don't understand what you want? – acw1668 Apr 16 '20 at 04:10
  • @acw1668 in the tutorial you get an input with the code `event, values = window.read()`. When you have multiple inputs in your window how do you separate them? – Thomas Apr 16 '20 at 15:18
  • You can assign a `key` to each widget and use this key to identify the data you want. For example, `sg.Multiline(..., key='textbox')`, then you can use `values['textbox']` to get the content of the `sg.Multiline`. – acw1668 Apr 16 '20 at 15:21
  • Thx @acw1668 . Wanna answer so I can give you a tick? – Thomas Apr 16 '20 at 15:22

1 Answers1

6

You can use sg.Multiline(...) which is Text widget of tkinter.

To get the content of the sg.Multiline, you can assign an unique key to it and use this key to get its content in the values dict.

Below is an example based on your code:

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()
acw1668
  • 40,144
  • 5
  • 22
  • 34