Tkinter Python Continuously update Label from serial data?

前端 未结 1 1507
无人及你
无人及你 2021-01-24 07:27

What I am trying to do with the following code is read from arduino serial and update a label with that data every few seconds.

When I run the code it only gets/updates

相关标签:
1条回答
  • 2021-01-24 08:13

    It appears that you misunderstand how the TK mainloop works. It is not, as you described, a loop between calling Tk() and mainloop(), but rather within Tkinter, external of your programs code.

    In order to have a loop, updating a label, you would have to specifically write a loop, using Tk's after method, calling an iterable function over and over.

    You could make a function like this to do what you want:

    def update_label():
        data= float(arduinoSerialData.readline())
    
        templabel.config(text=str(data)) #Update label with next text.
    
        Joes.after(1000, update_label)
        #calls update_label function again after 1 second. (1000 milliseconds.)
    

    I am unsure of how the arduino data is retrieved, so you may need to modify that slightly to have the correct data. This is a general premise though for creating a loop in the manner you described.

    0 讨论(0)
提交回复
热议问题