Python Forum
general def coding - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: general def coding (/thread-25503.html)



general def coding - Agusben - Apr-01-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?


RE: general def coding - DeaD_EyE - Apr-01-2020

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



RE: general def coding - Agusben - Apr-01-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


RE: general def coding - joe_momma - Apr-01-2020

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


RE: general def coding - Agusben - Apr-02-2020

  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


RE: general def coding - joe_momma - Apr-02-2020

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