0

So I'm trying to get the GUI up and running for a simple program that will check a modbus network. Which needs a giant text field to display data. I am using sg.Text. The problem I have is the text box doesn't append when updating. I am sure I am missing something obvious. I need to append because of how the program will check the network and then maybe save the entire text box. How do I make is scrollable as well? I feel like I having a massive brain fart.


import PySimpleGUI as sg
import os.path

# First the window layout in 2 columns

file_list_column = [
    [
        sg.Text("Search Network?"),
        sg.Button('Read Serial'),
        sg.Button('Test Modbus'),
    ],
    [
        sg.Text("Textbox test.", size=(30, 5),  relief=sg.RELIEF_SUNKEN, background_color='white', text_color='black',
            enable_events=True, key="-NETWORK LIST-")

    ],
    [
        sg.Input(key='-IN-')
    ],
    [
        sg.Button('Test'),
        sg.Button('Clear'),
        sg.Button('Exit')
    ],
]


# ----- Full layout -----
layout = [
    [
        sg.Column(file_list_column),
        sg.VSeperator(),
    ]
]

window = sg.Window("Network Troubleshooter", layout)

# Run the Event Loop
while True:
    event, values = window.read()
    if event == "Exit" or event == sg.WIN_CLOSED:
        break


    if event == 'Test':
        # Update the "output" text element to be the value of "input" element
        window['-NETWORK LIST-'].update(values['-IN-'])


    if event == 'Clear':
        # Update the "output" text element to be the value of "input" element
        window['-NETWORK LIST-'].update('')


window.close()
A_Bowler_Hat
  • 1
  • 1
  • 1

1 Answers1

4

sg.Text is a label, no append function. You can do it by adding string to the current value of the displayed text, then update it.

text = window['-NETWORK LIST-']
text.update(text.get()+'New Text append')

There's no scroll bar for element sg.Text, sg.Multiline is preferred and same way to append text.

Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • 1
    By the way, it's possible, with the tkinter version of PySimpleGUI, to set the Text element height to None and the result will be that the element always resizes to fit the contents. size of (20,None) for example. – Mike from PSG Dec 05 '20 at 19:40
  • 1
    Actually great tip there. Yes a disabled multiline is the way to go. I was using the wrong one. – A_Bowler_Hat Dec 07 '20 at 15:56