refreshing ui label text with constantly changing values

Viewed 47

What I want to do is keep refreshing these values. The status values don't change, but the result values do. Regardless, how can I update these values? Admittedly within python I have very little experience putting a GUI together regardless of the library. I've searched on how to do this but I can't really find anything definitive in my mind because one thing that absolutely stuck out was how the mainloop() method shouldn't be used in a loop.

def main():
    global last_str
    states = [
        {   
            'status': 'login screen',
            'threshold' : 70.0,
            'result' : 0.0,
            'template': 'assets/templates/login.jpg', 
            'dimensions': (0,0), 
            'location': (0,0),
        },
          ...
          ...
        {   
            'status': 'Loading Screen',
            'threshold' : 70.0,
            'result' : 0.0,
            'template': 'assets/templates/loading.jpg', 
            'dimensions': (0,0), 
            'location': (0,0),
        },
    ]

    window = tk.Tk()
    frame = tk.Frame(window,bd=5,relief=tk.SUNKEN)
    frame.pack()
    labels = {}
    i = 0
    for state in states:
        labels[states[i]['status']] = tk.Label(frame,
                                        text=states[i]['status'],
                                        font=('Arial',12),
                                        )
        labels[states[i]['status']].grid(column=0, row=i, sticky='W')

        labels[states[i]['result']] = tk.Label(frame,
                                        text=states[i]['result'],
                                        font=('Arial',12),
                                        )
        labels[states[i]['result']].grid(column=0, row=i, sticky='E')
        i+=1

    t = threading.Thread(target=window.mainloop())
    t.start()

    while True:
        weights = []
        for state in states:
            needle_base = Image.open(state['template'])
            state['dimensions'] = (needle_base.size)
            needle_base = convert_image(needle_base)
            (minVal, maxVal, minLoc, maxLoc) = find_needle_in_haystack(haystack, needle_base)
            state['result'] = maxVal * 100
            state['location'] = maxLoc
        
        # Reports which element of the array templates has the highest certainty'''
            status = state['status']
            threshold = state['threshold']
            certainty = state['result']
            weights.append(state['result'])

            if args.debug: labels[state['result']].configure(text=state['result'])
        
        # Get the highest scoring needle's index number from the array
        max_index = weights.index(max(weights))
        state_update('Status: ' + states[max_index]['status'])



if __name__ == "__main__":
    main()
0 Answers
Related