PySimpleGUI printing graph multiple times when threading my draw function

Viewed 14

I am trying to display a scatter plot after parsing some data and everything works great when I'm not threading my function like here:

if __name__ == '__main__':
    # define the window layout
    layout = [[sg.Input(key='-SECONDS-', size=(5,1)),
             sg.Button('Start Monitoring'),
             sg.Button('Exit', key="-EXIT-")],
              [sg.Text('Plot test', font='Any 18')],             
              [sg.Canvas(size=(500,500), key='canvas')]]


    window = sg.Window('XXXXXX Monitoring',
                       layout, finalize=True)

    fig_agg = None
    while True:
        event, values = window.read()
        if event is None:  # if user closes window
            break
        elif event.startswith('Start'):
            cycles = int(values['-SECONDS-'])
            print('Checking Thread! User selected {} cycles'.format(cycles))
            threading.Thread(target=start_monitoring, args=( cycles, window), daemon=True).start()    
        if event == "-THREAD-":
            print('Acquisition: ', values[event])
            time.sleep(1)
            if fig_agg is not None:
                delete_fig_agg(fig_agg)
            fig = fig_maker(window)
            fig_agg = draw_figure(window['canvas'].TKCanvas, fig)
            window.Refresh()
    
    window.close()

But as soon as I replace this code with an external function with threading(The line where it "-THREAD-"). The code keeps drawing the graphs forever right underneath each other without refreshing or removing the previous graph

if event == "-THREAD-":
     threading.Thread(target=threaded_GUI(), args=(window, fig_agg), daemon=True).start()

And here is the code that the threaded_GUI() function is referring to:

def threadedGUI(fig_agg, window):
    print('Acquisition: ', values[event])
    time.sleep(1)
    if fig_agg is not None:
        delete_fig_agg(fig_agg)
    fig = fig_maker(window)
    fig_agg = draw_figure(window['canvas'].TKCanvas, fig)
    window.Refresh()

So, basically any ideas on how to get my GUI to refresh properly instead of redrawing another graph right underneath with threading enabled?

0 Answers
Related