[Tkinter] 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: [Tkinter] general def coding (/thread-25487.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)+1) def stop(): stop_button=Button(root, text="Stop") stop_button.grid(row=0,column=2,padx=5,pady=5) 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() if stop_button == True: break 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()I need help with this function. how can I make a tkinter Button that stop/break a for loop in a function that draws with turtle? RE: general def coding - deanhystad - Apr-07-2020 Here's a way to stop the graph using the graph button. graphing = False def graph (): global graphing if graphing: graphing = False else: graphing = True pen.penup() angle = 0 theta = 0.01 steps = int ((100*pi/theta)+1) stop_plot = False 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() if not graphing: break graphing = FalseThe "graphing" variable is used to remember state; True while graphing, otherwise False. If you call graph while graphing == False, the pen begins drawing the graph. If you press the graph button again before the graph completes, graphing is set to False which ends the graph loop. If you really want a stop button the thing you need to do is make a global variable and set the value when you press the stop button. The important thing is the variable must be accessible by both the graph function and whatever function you call when the stop button is pressed. |