使用 tkinter 获取实时数据

发布于 2025-01-09 16:06:19 字数 1069 浏览 4 评论 0原文

我正在寻找一种简单的方法来在 python 的 GUI 中显示不断变化的实时数据。我连接到 2 台设备,想要不断显示数据(例如 20 个不同的值),当我按下按钮时,我想要控制一台设备。

不幸的是,我已经无法显示数据了。为此,我查看了一些 tkinter 教程和解释。

我的想法是用配置函数来实现它并连续覆盖标签。例如,我想如何显示一个值:

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()

不幸的是,该值在开始时只显示一次,之后没有任何变化。

另一个问题: 当我按下按钮时,我希望能够控制一台设备。为此,我有一个 while True 循环,它永久检查按钮是否被按下,然后执行操作。作为一个单独的程序没问题,但是如何将其集成到 tkinter GUI 中?当我启动 PyCharm 时总是崩溃。

我使用 PyCharm 和 Python 3.8

关于简单和功能性的想法我会很高兴,其他工具/模块等也一样,只要你可以轻松快速地实现这个想法。这只是一个研究项目,编程只是达到目的的一种手段。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木格 2025-01-16 16:06:20

您可以使用 tkinter 中的 after 方法在短暂延迟后运行某些内容。 GUI 准备就绪后,以下代码将运行 show_data,然后每 1000 毫秒运行一次。

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()

这解决了更新问题,我不确定当您按下按钮时您想要什么行为,但如果您详细说明和解释,我也许能够帮助并更新这个答案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文