##### general def coding
 general def coding Agusben Programmer named Tim Posts: 16 Threads: 10 Joined: Apr 2020 Reputation: Apr-01-2020, 02:55 PM ```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? Reply DeaD_EyE Da Bishop Posts: 1,711 Threads: 6 Joined: May 2017 Reputation: Apr-01-2020, 03:33 PM 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()``` Almost dead, but too lazy to die: https://sourceserver.info All humans together. We don't need politicians! Reply Agusben Programmer named Tim Posts: 16 Threads: 10 Joined: Apr 2020 Reputation: Apr-01-2020, 06:27 PM (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 Reply joe_momma Splitter Posts: 161 Threads: 7 Joined: Nov 2018 Reputation: Apr-01-2020, 09:54 PM 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- :) Reply Agusben Programmer named Tim Posts: 16 Threads: 10 Joined: Apr 2020 Reputation: 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 Reply joe_momma Splitter Posts: 161 Threads: 7 Joined: Nov 2018 Reputation: Apr-02-2020, 03:06 PM 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 Reply

 Possibly Related Threads… Thread Author Replies Views Last Post [Tkinter] general def coding Agusben 1 923 Apr-07-2020, 10:18 AM Last Post: deanhystad

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020