使用 tkinter 获取实时数据
我正在寻找一种简单的方法来在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 tkinter 中的
after
方法在短暂延迟后运行某些内容。 GUI 准备就绪后,以下代码将运行show_data
,然后每 1000 毫秒运行一次。这解决了更新问题,我不确定当您按下按钮时您想要什么行为,但如果您详细说明和解释,我也许能够帮助并更新这个答案。
You can use the
after
method in tkinter to run something after a short delay. The following code will runshow_data
once the GUI is ready and then again every 1000 milliseconds.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.