tkinter python心率和动画

tkinter python心率和动画,python,animation,tkinter,raspberry-pi,Python,Animation,Tkinter,Raspberry Pi,我正试图用树莓皮制作一个以心率日期作为触发器的动画。 我已经使用了github代码,并且正在使用example.py作为我工作的基础 我还使用了一个很好的教程,使用tkinter可以让球在屏幕上移动 我是python的新手,在if语句中播放动画时遇到了麻烦。理想情况下,我希望球以bmp数据生成的速度移动,这样更快的心率将使球移动得更快。我现在似乎离这很远。 如果有人能帮助这项工作,我将永远感激。当它站立时,球出现但不移动。我认为这与运动和bpm更新有冲突 代码如下: from pulsesens

我正试图用树莓皮制作一个以心率日期作为触发器的动画。 我已经使用了github代码,并且正在使用
example.py
作为我工作的基础

我还使用了一个很好的教程,使用tkinter可以让球在屏幕上移动

我是python的新手,在if语句中播放动画时遇到了麻烦。理想情况下,我希望球以bmp数据生成的速度移动,这样更快的心率将使球移动得更快。我现在似乎离这很远。 如果有人能帮助这项工作,我将永远感激。当它站立时,球出现但不移动。我认为这与运动和bpm更新有冲突

代码如下:

from pulsesensor import Pulsesensor
import time
from tkinter import *
import random

tk = Tk()
WIDTH=1500
HEIGHT=800
canvas = Canvas(tk, bg="brown4", height=HEIGHT, width= WIDTH)
tk.title("drawing")
canvas.pack()

##below is the class to create multiple balls that are coloured
##and move and detect the edge and bounce

class Ball:
    def __init__(self, color, size):
        self.shape  = canvas.create_oval (10, 10, size,  size, fill=color, 
                                          outline=color, stipple="gray25")
        self.xspeed = random.randrange(-1,5)
        self.yspeed = random.randrange(-1,5)

    def move(self):
        canvas.move(self.shape, self.xspeed, self.yspeed)
        pos = canvas.coords(self.shape)
        if pos[3]>=HEIGHT or pos[1]<=0:
            self.yspeed=-self.yspeed
        if pos[2] >=WIDTH or pos[0] <=0:
            self.xspeed=-self.xspeed


colors=["red4", "red3", "OrangeRed2","OrangeRed4","firebrick3"]
##this is make 100 balls
balls=[]
##this is to set the colour and size of the balls which is randomised:
for i in range (100):
    balls.append(Ball(random.choice(colors), random.randrange(150, 200)))

##this is to call the balls
##while True:

p = Pulsesensor()
p.startAsyncBPM()

try:
    while True:
        bpm = p.BPM
        if bpm > 0:
            print("BPM: %d" % bpm)
            for ball in balls:
                    ball.move()
            tk.update()
            time.sleep(0.02)
            tk.mainloop()
        else:
            print("No Heartbeat found")
        time.sleep(1)
except:
    p.stopAsyncBPM()
从脉冲传感器导入脉冲传感器
导入时间
从tkinter进口*
随机输入
tk=tk()
宽度=1500
高度=800
画布=画布(tk,bg=“brown4”,高度=高度,宽度=宽度)
标题(“图纸”)
canvas.pack()
##下面是创建多个彩色球的类
##移动并检测边缘和反弹
班级舞会:
定义初始值(自身、颜色、大小):
self.shape=canvas.create_oval(10,10,大小,大小,填充=颜色,
轮廓=颜色,点画=“灰色25”)
self.xspeed=random.randrange(-1,5)
self.yspeed=random.randrange(-1,5)
def移动(自我):
canvas.move(self.shape、self.xspeed、self.yspeed)
pos=画布坐标(自形)
如果位置[3]>=高度或位置[1]=宽度或位置[0]0:
打印(“BPM:%d”%BPM)
对于球中球:
球移动
tk.update()
睡眠时间(0.02)
tk.mainloop()
其他:
打印(“未找到心跳”)
时间。睡眠(1)
除:
p、 stopAsyncBPM()

<代码> > p>我没有树莓PI或脉冲传感器,所以下面的测试只能在一定程度上进行,但希望它能为你提供更好的基础代码。我减少了球的数量,以便更容易看到发生了什么。我真的不明白他们的运动是如何与脉搏率联系在一起的,所以你需要自己去充实一下

在使用tkinter时,重要的是要了解发生的一切都必须通过
mainloop()
来完成,它通常在应用程序脚本退出之前不会返回(因此它本质上是一个无止境的循环)。这意味着你通常不能想什么时候就叫它。在这种情况下,将使用一个“polling”函数,并以设定的间隔调用该函数以更新
Canvas
对象,这一切都将在
mainloop()
运行时发生

from pulsesensor import Pulsesensor
import random
from tkinter import *

WIDTH=1500
HEIGHT=800
COLORS = ["red4", "red3", "OrangeRed2", "OrangeRed4", "firebrick3"]
DELAY = 200  # In millisecs
NUMBALLS = 5

class Ball:
    def __init__(self, color, size):
        self.shape = canvas.create_oval(10, 10, size,  size, fill=color,
                                        outline=color, stipple="gray25")
        self.xspeed = random.randrange(-1, 5)
        self.yspeed = random.randrange(-1, 5)

    def move(self):
        canvas.move(self.shape, self.xspeed, self.yspeed)
        pos = canvas.coords(self.shape)
        if pos[3] >= HEIGHT or pos[1] <= 0:
            self.yspeed = -self.yspeed
        if pos[2] >= WIDTH or pos[0] <= 0:
            self.xspeed = -self.xspeed

def poll(p):
    try:
#        bpm = p.BPM
        bpm = random.randint(0, 200)  # Random value for testing.
        if bpm < 1:
#            print("No Heartbeat found")
            pass
        else:
#            print("BPM: %d" % bpm)
            for ball in balls:
                ball.move()
    except Exception as exc:
        print('Exception raised: {}'.format(exc))
        p.stopAsyncBPM()
        root.quit()

    root.after(DELAY, poll, p)  # Call this function again after delay.

if __name__ == '__main__':
    root = Tk()
    root.title("Beating Heart")
    canvas = Canvas(root, bg="brown4", height=HEIGHT, width=WIDTH)
    canvas.pack()
    balls = [Ball(random.choice(COLORS), random.randrange(150, 200))
                for _ in range(NUMBALLS)]

    p = Pulsesensor()
    p.startAsyncBPM()
    poll(p)  # Start polling.
    root.mainloop()
从脉冲传感器导入脉冲传感器
随机输入
从tkinter进口*
宽度=1500
高度=800
颜色=[“红色4”、“红色3”、“橙色2”、“橙色4”、“耐火砖3”]
延迟=200毫秒
Nuballs=5
班级舞会:
定义初始值(自身、颜色、大小):
self.shape=canvas.create_oval(10,10,大小,大小,填充=颜色,
轮廓=颜色,点画=“灰色25”)
self.xspeed=random.randrange(-1,5)
self.yspeed=random.randrange(-1,5)
def移动(自我):
canvas.move(self.shape、self.xspeed、self.yspeed)
pos=画布坐标(自形)

如果位置[3]>=高度或位置[1]=宽度或位置[0],您在哪里更改球的速度?好像什么地方都没有。嗨,罗布,谢谢你的回复。我还没有改变速度。这是最终的目标,但我认为我给这个班打电话是做错了什么?你选了一个不好的动画教程。谢谢你,布莱恩,我现在正试图弄清楚,可悲的是,除了你调用函数的方式之外,我无法理解与我所拥有的不同之处。这没有按以下方式工作:try:while True:bpm=p.bpm如果bpm>0:ball.move()root.mainloop()其他:打印(“未找到心跳”)时间。sleep(1)您不需要调用
poll
内部的
更新\u idletasks
。更新会在
poll
返回并再次运行之间自动进行。@Bryan:已删除。我不确定是否真的需要,所以把它放进去以防万一。再次感谢你的提示,谢谢你们的帮助。我会让你知道,一旦我完成了它,并希望分享的结果!所有的best@Tina:不客气。如果它能起作用并帮助你实现你的目标,请考虑接受我的答案。再见,谢谢你的帮助。我还有一个问题,但似乎无法将代码粘贴到这里,因此我将尝试启动一个新线程。再次感谢。