python code problem

45 views
Skip to first unread message

Tina Burton

unread,
Oct 9, 2017, 4:15:04 PM10/9/17
to Cambridge and East Anglian Python Users Group
Hello, 
I am hoping someone can help with the following code. 
It is displaying the balls, but they do not move. 
I have the feeling there is some kind of conflict..
The code calls in another file, which works properly, but when I add the animation class it doesn't. 
Many thanks for any help or advice.
all the best
Tina


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=[]

for i in range (100):

##                this is to set the colour and size of the balls which is randomised, so they can 

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

Jakub Czaplicki

unread,
Oct 9, 2017, 5:12:43 PM10/9/17
to cam...@googlegroups.com
Hi Tina,

That's because `tk.mainloop()` halts your program. Try commenting out
tk.mainloop() and see what happens.

I am guessing you're learning programming, so I won't comment on the
overall code :)

You may also want to check :
https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop
and
https://stackoverflow.com/questions/13215215/python-tkinter-animation

Jakub
> --
> You received this message because you are subscribed to the Google Groups
> "Cambridge and East Anglian Python Users Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to campug+un...@googlegroups.com.
> To post to this group, send email to cam...@googlegroups.com.
> Visit this group at https://groups.google.com/group/campug.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages