0

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.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Stressor
  • 1
  • 2

1 Answers1

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.

scotty3785
  • 6,763
  • 1
  • 25
  • 35