'Real Time Data with tkinter

I am looking for a simple way to display changing real time data in a GUI in python. I am connected to 2 devices and want to display data constantly (like 20 different values), and when I press a button I want to control the one device.

Unfortunately I fail already with the display of the data. For this I have looked at some tkinter tutorials and explanations.

My idea was to implement it with a config function and to overwrite the label continuously. As example how I wanted to display one value:

import tkinter as tk
from pydualsense import pydualsense

# connect to the device
dualsense = pydualsense()
dualsense.init()

# create a window
window = tk.Tk()

# function for updating data
def show_data():
    global dualsense
    data_label_output.config(text=dualsense.state.LX)

# showing the data as a lable
data_label_output = tk.Label(window)
data_label_output.grid(row=1, column=1)
show_data()


#### or different solution 
# showing the data as a lable
data_label_output = tk.Label(window, comand=show_data)
data_label_output.grid(row=1, column=1)

window.mainloop()

Unfortunately, the value is displayed only once at the beginning and nothing changes after that.

Another problem: When I press the button, I want to be able to control the one device. For this I have a while True loop that permanently checks if a button is pressed and then executes actions. As a separate program no problem, but how do I integrate this into the tkinter GUI? When I start this PyCharm always crashes.

I use PyCharm and Python 3.8

About simple and functional ideas I would be happy, also to other tools/modules etc., as long as you can easily and quickly implement the idea. It's only for a research project and the programming is only a means to an end.



Solution 1:[1]

You can use the after method in tkinter to run something after a short delay. The following code will run show_data once the GUI is ready and then again every 1000 milliseconds.

import tkinter as tk
from pydualsense import pydualsense

# connect to the device
dualsense = pydualsense()
dualsense.init()

# create a window
window = tk.Tk()

# function for updating data
def show_data():
    global dualsense
    data_label_output.config(text=dualsense.state.LX)
    window.after(1000,show_data)

# showing the data as a lable
data_label_output = tk.Label(window)
data_label_output.grid(row=1, column=1)  

window.after_idle(show_data)
window.mainloop()

This resolves the updating issue, I'm not sure what behaviour you want when you press the button but if you elaborate and explain, I might be able to help and update this answer.

Solution 2:[2]

Thank you very much, it helped me a lot. The Problem is, that i have like a functions running in the Backgound all the time. In my main Programm I have a "while True:" and than i call like 4-5 funktions and need them to run constantly, and in that loop i want to update the GUI, or i want that the "while True:" loop runs in the background.

If for example i take your Code and put something easy as a Example:

while True:
      x = 1
      x +=1
      print(x)

when i put the loop at the end of the code, it runs only if i close the windows. If i put the loop bevor the windows.mainloop() it runs bevor the windows opens and the windows dont open. And when i put it in the while loop it just printed 2 and then the window is running.

Like what i Imaging is: I Create a Window once, and in a while loop, i have a function whiches updates the Values in the Labels. Is that possible?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 scotty3785
Solution 2 Stressor