简体   繁体   中英

Dynamically change row size PySimpleGUI

I am trying to dynamically display rows in the UI when a button is pressed.

To do this, I am first declaring the row in the layout, but making it hidden. Then when I get the value, I make the row hidden.

Sometimes the text is very large, so I am using the textwrap's fill method in python. So this gives me text with 2 to 3 rows.

But when I try to, each character appears in different rows.

disp_text = "Text:\n{}".format(textwrap.fill(some_text,70))

size = (70,disp_text.count("\n")+1)

window['-TEXT-'].set_size(size)

window['-TEXT-'].update(value = disp_text,visible = True)

Assume some_text is "Hello how are you....(and some 50 other characters)"

Now, with the above string, the size is (70,3)

So, ideally it should wrap some_text into 3 lines.

But the actual output in the GUI is:

H
e
l

Which is basically the first 3 characters in some_text in 3 seperate lines instead of some_text as a while in 3 different lines.

How does one fix this?

Print your disp_text to make sure it's got what you think it does.

Here's one approach.

import PySimpleGUI as sg

def main():
    layout = [  [sg.Text('My Window')],
                [sg.Text(size=(20,1), auto_size_text=False, key='-OUT-')],
                [sg.Button('Go'), sg.Button('Exit')]  ]

    window = sg.Window('Window Title', layout)

    while True:             # Event Loop
        event, values = window.read()
        print(event, values)
        if event == sg.WIN_CLOSED or event == 'Exit':
            break
        if event == 'Go':
            window['-OUT-'].set_size((20, 10))
            window['-OUT-'].update('This is my output string\nThat spans multiple rows\nAll the way to 3\n'+
                                   'It will even wrap if the string is too long')
            wraplen = window['-OUT-'].Widget.winfo_reqwidth()  # width tkinter says widget will be
            window['-OUT-'].Widget.configure(wraplen=wraplen)  # set wrap to width of widget to match contents

    window.close()

if __name__ == '__main__':
    main()

休息窗口

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM