Java如何隐藏控制按键动画,如何通过按下按钮来启动和停止动画 . 功能动画?...

我是python编程的新手,很抱歉,如果这是一个愚蠢的问题 .

这里,使用动画功能来绘制数据 . 我想通过按下gui上的按钮来启动和停止这个animationFunc,但不知道如何实现它 .

我将不胜感激任何帮助!谢谢

这是代码:

import Tkinter as tk

import matplotlib

matplotlib.use("TkAgg")

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,

NavigationToolbar2TkAgg

from matplotlib.figure import Figure

import matplotlib.animation as animation

from matplotlib import style

import serial

import sys

ser = serial.Serial(port='COM3', baudrate = 19200, bytesize=8, parity='N',

stopbits=1, timeout=None,rtscts=0)

LARGE_FONT = ("Verdana", 12)

style.use('ggplot')

f = Figure(figsize=(5,5), dpi = 100)

a = f.add_subplot(111)

yList = []

def animate(i):

# I receive data as a string with a hex number, so here I read it, take

#the number and append it to the yList

r = ser.readline()

he = r.split(" ")[2]

g = int(he,16)

d = str(g)

yList.append(g)

a.clear()

a.plot(yList)

class Sensors(tk.Tk):

def __init__(self,*args, **kwargs ):

tk.Tk.__init__(self,*args,**kwargs)

container = tk.Frame(self)

container.pack(side="top", fill = "both", expand = True)

container.grid_rowconfigure(0, weight=1)

container.grid_columnconfigure(0, weight=1)

self.frames = {}

for F in (ChooseSensor, Sensor3):

frame = F(container, self)

self.frames[F] = frame

frame.grid(row=0, column=0, sticky="nsew")

self.show_frame(ChooseSensor)

def show_frame(self, cont):

frame = self.frames[cont]

frame.tkraise()

#here I want to define the function to stop the animate function and to

#stop the serial port

def stopPlot(event,cont):

#command to serial port to stop sensor

ser.write(b'S1,P,0\r\n')

#here i want to create the function to start the animate function

def animate_start(self,cont):

#command to configure and start sensor

ser.write(b'S1,S,2\r\n')

ser.write(b'S1,T,1\r\n')

ser.write(b'S1,P,1\r\n')

class ChooseSensor(tk.Frame):

def __init__(self, parent, controller):

tk.Frame.__init__(self,parent)

label = tk.Label(self, text = "Choose Sensor", font= LARGE_FONT)

label.pack(pady=10, padx=10)

button1 = tk.Button(self,text="Graph Page",

command=lambda:controller.show_frame(Sensor3))

button1.pack()

class Sensor3(tk.Frame):

def __init__(self,parent,controller):

tk.Frame.__init__(self,parent)

label = tk.Label(self, text = "Graph Page", font= LARGE_FONT)

label.pack(pady=10, padx=10)

button1 = tk.Button(self,text="Start Plot",

command=lambda:controller.animate_start(self))

button1.pack()

button2 = tk.Button(self,text="Stop Plot",

command=lambda:controller.stopPlot(self))

button2.pack()

button3 = tk.Button(self,text="Back to Home",

command=lambda:controller.show_frame(ChooseSensor))

button3.pack()

canvas = FigureCanvasTkAgg(f, self)

canvas.show()

canvas.get_tk_widget().pack(side=tk.TOP, fill = tk.BOTH, expand = True)

app = Sensors()

ani = animation.FuncAnimation(f,animate, interval=1000)

app.mainloop()

编辑:我试图做这样的事情:

def animate_start(self,cont):

global anim_start

anim_start=True

#Konfiguriert und schreibt Sensor1

ser.write(b'S1,S,2\r\n')

ser.write(b'S1,T,1\r\n')

ser.write(b'S1,P,1\r\n')

return anim_start

def stopPlot(event,cont):

anim_start=False

ser.write(b'S1,P,0\r\n')

return anim_start

然后只在anim_start == True时执行动画功能:

app = Sensors()

if anim_start == True:

ani = animation.FuncAnimation(f,animate, interval=1000)

app.mainloop()

现在我可以看到,发送命令并接收数据(USB串口桥上的LED指示灯闪烁),但是没有数据添加到yList,因此没有绘制图表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值