Search code examples
pythonscrollbarpysimplegui

python table set_vscroll_postion not working


Running Python pysimplegui with a table im not exacly sure of where to put my .set_vscroll_postion(1.0)

My main issue is that i have lots of data and I want the user to see the most current.

I imagine i could either:

  1. reverse my contents of the table (if possible). 2.Have the scroll bar help by showing the most current
headings = ['Time Stamp','(MB)TempF', '(MB)Humidity', 'etc']
measured_values_array = [
    ['Begin Info']
]


layout = [
        [gui.Table(values=measured_values_array, 
        headings=headings, 
        max_col_width=35,
                    auto_size_columns=True,
                    display_row_numbers=False,
                    justification='left',
                    num_rows=10,
                    key='CLK',
                    row_height=18,alternating_row_color="grey",
                    )]
    ]
 

window = gui.Window('Display Values Differant Methods', layout, margins=(250, 250),font=('Arial', 12), finalize=True)


ui.Table.set_vscroll_postition(1.0)

while True:

    event, values = window.read(timeout=100)

    if event in (None, gui.WIN_CLOSED):
        break

    if event == gui.TIMEOUT_EVENT:
        time.sleep(2)
        ComPort = serial.Serial("COM3", baudrate=9600,bytesize=8,parity='N',stopbits=1, timeout=1,)
        Temphex = b'\x01\x03\x00\x00\x00\x02\xC4\x0B'
        ComPort.write(Temphex)
        temp = ComPort.read(7)
        t=" ".join(["{:02x}".format(x) for x in temp])
        t1=(( int( (t[9:11]+t[12:15]),16))/100)
        tf = round((t1*(9/5))+32,2)
        ComPort.close()
        
        ComPort = serial.Serial("COM3", baudrate=9600,bytesize=8,parity='N',stopbits=1, timeout=1,)
        Humhex = b'\x01\x03\x00\x01\x00\x02\x95\xCB'
        ComPort.write(Humhex)
        hum = ComPort.read(7)
        h=" ".join(["{:02x}".format(x) for x in hum])
        h1=(( int( (h[9:11]+h[12:15]),16))/100)
        ComPort.close()
        
        current_time = datetime.datetime.now().strftime('%m-%d-%y %H:%M:%S%p')
                
        currentTemp = str(tf)
        currentHum = str(h1)
        measured_values_array.append([current_time,tf,h1])     
        window['CLK'].update(measured_values_array)
     

window.close()


Solution

  • Call method set_vscroll_postition(1.0) after you update the Table element.

    Demo Code

    from random import randint
    from datetime import datetime
    import PySimpleGUI as sg
    
    def now():
        return datetime.now().strftime('%m-%d-%y %H:%M:%S%p')
    
    def new_data():
        return [now(), randint(0, 50), randint(60, 85), '']
    
    headings = ['Time Stamp','(MB)TempF', '(MB)Humidity', 'etc']
    measured_values_array = [new_data() for i in range(10)]
    
    sg.set_options(font=("Courier New", 12))
    layout = [
        [sg.Table(
            values=measured_values_array,
            headings=headings,
            auto_size_columns=False,
            col_widths=[22, 14, 14, 20],
            justification='center',
            num_rows=10,
            alternating_row_color="grey",
            key='CLK',
        )],
        [sg.Push(), sg.Button("New Data")],
    ]
    window = sg.Window('Display Values Differant Methods', layout, finalize=True)
    
    while True:
    
        event, values = window.read()
    
        if event == sg.WIN_CLOSED:
            break
    
        elif event == "New Data":
            measured_values_array.append(new_data())
            window['CLK'].update(measured_values_array)
            window['CLK'].set_vscroll_position(1.0)
    
    window.close()
    

    enter image description here