Posts: 16
Threads: 10
Joined: Apr 2020
from tkinter import *
import turtle
from math import cos,sin,tan,pi
root = Tk()
def graph ():
pen.penup()
angle = 0
theta = 0.01
steps = int ((100*pi/theta))
for t in range(0,steps):
a = 1
b = 5
c = 6
angle+=theta
x=(cos(a*angle)+cos(b*angle)+cos(c*angle))*100
y=(sin(a*angle)+sin(b*angle)+sin(c*angle))*100
pen.goto(x,y)
pen.pendown()
canvas=turtle.Canvas(master=root, width=650, height=650)
canvas.grid(row=0, column=0)
pen=turtle.RawTurtle(canvas)
graph_button=Button(root, text="Graph", command=graph)
graph_button.grid(row=0,column=1,padx=5,pady=5)
stop_button=Button(root, text="Stop")
stop_button.grid(row=0,column=2,padx=5,pady=5)
root.mainloop() How can i make the stop_button break/stop the for loop in the graph def?
Posts: 2,125
Threads: 11
Joined: May 2017
Minimal change:
from tkinter import *
import turtle
from math import cos,sin,tan,pi
from threading import Event
root = Tk()
stop = Event()
def graph ():
# reset stop event, if it was already set
stop.clear()
pen.penup()
angle = 0
theta = 0.01
steps = int ((100*pi/theta))
for t in range(0,steps):
if stop.is_set():
break
a = 1
b = 5
c = 6
angle+=theta
x=(cos(a*angle)+cos(b*angle)+cos(c*angle))*100
y=(sin(a*angle)+sin(b*angle)+sin(c*angle))*100
pen.goto(x,y)
pen.pendown()
canvas=turtle.Canvas(master=root, width=650, height=650)
canvas.grid(row=0, column=0)
pen=turtle.RawTurtle(canvas)
graph_button=Button(root, text="Graph", command=graph)
graph_button.grid(row=0,column=1,padx=5,pady=5)
stop_button=Button(root, text="Stop", command=stop.set)
stop_button.grid(row=0,column=2,padx=5,pady=5)
Button(root, text="Exit", command=root.destroy).grid(row=0,column=3,padx=5,pady=5)
root.mainloop()
Posts: 16
Threads: 10
Joined: Apr 2020
(Apr-01-2020, 03:33 PM)DeaD_EyE Wrote: Minimal change:
from tkinter import *
import turtle
from math import cos,sin,tan,pi
from threading import Event
root = Tk()
stop = Event()
def graph ():
# reset stop event, if it was already set
stop.clear()
pen.penup()
angle = 0
theta = 0.01
steps = int ((100*pi/theta))
for t in range(0,steps):
if stop.is_set():
break
a = 1
b = 5
c = 6
angle+=theta
x=(cos(a*angle)+cos(b*angle)+cos(c*angle))*100
y=(sin(a*angle)+sin(b*angle)+sin(c*angle))*100
pen.goto(x,y)
pen.pendown()
canvas=turtle.Canvas(master=root, width=650, height=650)
canvas.grid(row=0, column=0)
pen=turtle.RawTurtle(canvas)
graph_button=Button(root, text="Graph", command=graph)
graph_button.grid(row=0,column=1,padx=5,pady=5)
stop_button=Button(root, text="Stop", command=stop.set)
stop_button.grid(row=0,column=2,padx=5,pady=5)
Button(root, text="Exit", command=root.destroy).grid(row=0,column=3,padx=5,pady=5)
root.mainloop()
thanks
this is very helpful
Posts: 165
Threads: 7
Joined: Nov 2018
Quote:from math import cos,sin,tan,pi
theta = 0.01
steps = int ((100*pi/theta))
how many steps is that? well in my prompt:
>>> from math import cos,sin,tan,pi
>>> theta = 0.01
>>> steps = int ((100*pi/theta))
>>> steps
31415 how many do you need to make it back to your start point?
>>> from math import cos,sin,tan,pi
>>> theta = 0.01
>>> steps = int ((2.01*pi/theta))
>>> steps
631 it's always nice to have an emergency stop button- :)
Posts: 16
Threads: 10
Joined: Apr 2020
Apr-02-2020, 12:12 AM
(This post was last modified: Apr-02-2020, 12:13 AM by Agusben.)
angle = 0
theta = 0.01
steps = int ((100*pi/theta))
for t in range(0,steps):
if stop.is_set():
break
a = 1
b = 5
c = 6
angle+=theta
x=(cos(a*angle)+cos(b*angle)+cos(c*angle))*100
y=(sin(a*angle)+sin(b*angle)+sin(c*angle))*100
pen.goto(x,y)
pen.pendown() the thing is that i don't know how to calculate the number of steps needed for this to finish.
x=(cos(a*angle)+cos(b*angle)+cos(c*angle))*100
y=(sin(a*angle)+sin(b*angle)+sin(c*angle))*100 is thre a way to calculate it?
it would be great to know if so
Posts: 165
Threads: 7
Joined: Nov 2018
I didn't calculate it python did, took your start point x,y
Quote:x=(cos(a*angle)+cos(b*angle)+cos(c*angle))*100
y=(sin(a*angle)+sin(b*angle)+sin(c*angle))*100
made a variable to count iterations, made a loop, created a function to check for the start point, stopped the loop- then rounded the count. You can do it too that's what's great about the turtle module- play. then take on fractals
|