python - 有没有办法在 Tkinter(python)中为 matplotlib 设置可变间隔?
问题描述
我正在尝试绘制从连接到 Arduino 的体重秤接收到的读数。使用蓝牙的 Arduino 将数据发送到 RPi,我成功地制作了一个实时 GUI。问题是,我正在尝试使用 SpinBox 或某种计数器来调整图形更新的时间间隔。我尝试在 FuncAnimation 中使用间隔变量,但它似乎不起作用。我已经蛮力并注意到一旦代码被执行,间隔被设置为它接收并坚持它的初始值。While 循环不适用于我必须实现的目标。我也附上了下面的代码。请注意,我是 Python 的初学者。
import serial
from tkinter import *
from tkinter import ttk
from matplotlib import pyplot as plt
import matplotlib.animation as animation
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import style
from matplotlib.figure import Figure
import time
import read_arduino
ser = serial.Serial('/dev/rfcomm0', 115200)
ser.flushInput()
root = Tk()
root.geometry('800x400+200+100')
root.title('This is my root window')
tab_control = ttk.Notebook(root)
tab1 = ttk.Frame(tab_control)
tab2 = ttk.Frame(tab_control)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')
# var =IntVar()
# var.set(300)
# incr=1000
# spin = Spinbox(tab1, from_=200, to=1000, width=5, textvariable=var)
xar = [0]
yar = [0]
style.use('ggplot')
fig = plt.figure(figsize=(3, 2.5))
plt.xlabel("Time")
plt.ylabel("Weight")
ax1 = fig.add_subplot(1,1,1)
ax1.set_aspect("auto")
fig.subplots_adjust(wspace=0, hspace=0)
ax1.set_ylim(0, 100)
line, = ax1.plot(xar, yar, 'blue',marker='x')
plt.tight_layout()
dum= StringVar()
lbl = Label(tab1, textvariable=dum).grid(row=2,column=2)
def animate(i):
x=get_avg()
yar.append(x)
xar.append(i)
line.set_data(xar, yar)
ax1.set_xlim(0, i+1)
dum.set(x)
def get_avg():
n = 60
val=str(0.0)
for n in range(1,60):
val=str(ser.readline(),"utf-8").strip("\n")
return val
plotcanvas = FigureCanvasTkAgg(fig, tab1)
plotcanvas.get_tk_widget().grid(row=5,column=0)
ani = animation.FuncAnimation(fig, animate, interval=1000, blit=False)
tab_control.pack(expand=1, fill='both')
root.mainloop()
解决方案
推荐阅读
- java - 与哨兵分组
- python - 如何在 python 中为 for 循环手动输入?
- android - 为什么我的应用启动后 WorkManager 会被触发多次?
- jenkins - 如何从经典 ASP 运行 Jenkins 作业?
- c++ - 帧缓冲区未将深度渲染到深度纹理中
- jquery - 创建 3 个在单击时展开并显示内容的 Div
- angular - FormGroup.reset() 不会更新 DOM 中的值
- html - 类旁边的选择器是什么?
- ruby - 使用“to_sym”方法有什么意义?
- javascript - 如何在 BotKit 中存储和访问来自外部 API 请求的对话变量?