Python Forum
general def coding
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
general def coding
#1
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
#2
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
#3
(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
#4
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
#5
  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
#6
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