RayEagle
RayEagle

Reputation: 13

Automatically add a scroll in Multiline when the number of rows is exceeded

Tell me how to automatically connect scrolling in PySimpleGUI Multiline when the number of lines entered exceeds. For example, to enable scrolling when there are more than 5 lines

sg.Multiline(size=(42, 5))

Upvotes: 0

Views: 866

Answers (2)

Jason Yang
Jason Yang

Reputation: 13061

The method is to hack the method set for the tk.Scrollbar, and it is only work for the vertical scrollbar for following code.

import PySimpleGUI as sg

def scrollbar_set(self, lo, hi):
    if float(lo) <= 0.0 and float(hi) >= 1.0:
        self.pack_forget()
    elif self.cget("orient") != sg.tk.HORIZONTAL:
        self.pack(side=sg.tk.RIGHT, fill=sg.tk.Y)
    self.old_set(lo, hi)

sg.tk.Scrollbar.old_set = sg.tk.Scrollbar.set
sg.tk.Scrollbar.set     = scrollbar_set

layout = [
    [sg.Text('Auto hide scrollbar', size=25)],
    [sg.Multiline(size=(20, 5), expand_x=True, expand_y=True, key='-ML-')]]
window = sg.Window('Title', layout, finalize=True)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

window.close()

Upvotes: 0

RayEagle
RayEagle

Reputation: 13

import PySimpleGUI as sg


def begin_window():
    layout = [[sg.Multiline(size=(42, 5), autoscroll=True)]]

    # Create the Window
    window = sg.Window('Title', layout)
    # window['-INDEX-'].bind("<Return>", "_Enter")
    # Event Loop to process "events" and get the "values" of the inputs
    while True:
        event, values = window.read()

        if event == sg.WIN_CLOSED or event == '-CANCEL-':  # if user closes window or clicks cancel
            break

    window.close()

    return


if __name__ == '__main__':
    begin_window()

Scroll appears immediately, when the field is empty enter image description here

Upvotes: 1

Related Questions