I wrote this code which creates an oval in tkinter. When clicked on by a mouse, the oval creates new coordinates for itself and then starts pulsating.
It goes like this: 1.the oval is created by default 2.On click, a function "click" is called 3.The click function generates new coordinates, draws the oval and then starts the pulsating looping effect. 4.Now I should be able and can click again on the pulsating oval, to move the oval to a new location and then loop that new oval again(pulsate).
def click(event):
pick = 2
counter = 0
esimene_x1 = randint(0, w-100)
esimene_y1 = randint(0, h-100)
teine_x1 = esimene_x1
teine_y1 = esimene_y1
canvas.coords(circle1, esimene_x1, esimene_y1, teine_x1, teine_y1)
pulsate(esimene_x1, esimene_y1, teine_x1, teine_y1, pick, counter)
def pulsate(esimene_x1, esimene_y1, teine_x1, teine_y1, pick, counter):
if pick % 2 == 0:
esimene_x1 -= 1
esimene_y1 -= 1
teine_x1 += 1
teine_y1 += 1
counter += 1
if counter == 40:
pick += 1
elif pick % 2 != 0:
esimene_x1 += 1
esimene_y1 += 1
teine_x1 -= 1
teine_y1 -= 1
counter -=1
if counter == 0:
pick += 1
s = esimene_x1, esimene_y1, teine_x1, teine_y1, pick, counter
canvas.coords(circle1, esimene_x1,esimene_y1, teine_x1,teine_y1)
raam.after(50, pulsate, *s)
However, when I click again on the pulsating oval now, it kinda bugs and it looks like the previous function for the looping pulsating effect is still doing its thing and the new pulsating begins at different coordinates. So, my question is, how do I stop the function from looping(pulsating), when that function is in a function, and i want to start the first function again. (Create, new coordinates and start the pulsating over)
Here is the file that you can run and see exactly what seems to happen:
https://mega.co.nz/#!e5pj0brC!QW6R4X9WTshOCh3FTybLrQu_oI0OOU6wL5QI61punUE
Also, if you see anything else that seems to be causing this bug, then let me know please. I have not yet acquired knowledge of threading and classes in python, but if that is the only solution then please let me know! Else would appreciate if it can be managed with some easier methods!
Thank you very much!