Stop Button in Tkinter

2024/10/8 8:27:17

I'm trying to have a turtle animation start with a button and stop with a button. It's very easy to start with a button but I can't seem to be able to figure out a stop button? Here's my code so far:

import turtle
import tkinter as tk
def start():t.forward(100)t.right(90)t.forward(100)t.left(90)t.forward(100)t.right(90)t.forward(100)t.right(90)t.forward(100)def stop():t.stopdef clear():canvas.delete("all")root = tk.Tk()
canvas = tk.Canvas(width = 500, height = 500)
canvas.pack()t = turtle.RawTurtle(canvas)tk.Button(text = "Start", command = start).pack(side = tk.LEFT)
tk.Button(text = "Stop", command = stop).pack(side = tk.LEFT)
tk.Button(text = "Clear", command = clear).pack(side = tk.LEFT)root.mainloop()

Also the clear button works but afterwards the start button doesn't work anymore. If someone can help me with that as well.

Thank you to @Mike - SMT for helping me with this code. Here's the edited and fully functioning code:

import turtle
import tkinter as tkdef start(turtle_object, draw_path):global tracker, start_ndex, end_ndex, startedtracker = Falseif started == False:started = Truefor i in range(start_ndex, end_ndex):if tracker == False and i <= end_ndex:pth = draw_path[i]if pth[0] == "f":turtle_object.forward(pth[1])elif pth[0] == "r":turtle_object.right(pth[1])elif pth[0] == "l":turtle_object.left(pth[1])start_ndex += 1running = Truedef stop():global tracker, startedtracker = Truestarted = Falsedef clear():global t, tracker, started, start_ndext.reset()start_ndex = 0started = Falset = turtle.RawTurtle(canvas)root = tk.Tk()
tracker = False
start_ndex = 0
started = False # added this tracking variable to prevent issues with     spamming the start button.draw_path = [["f", 100], ["r", 90], ["f", 100], ["l", 90], ["f", 100], ["r", 90], ["f", 100], ["r", 90], ["f", 100]]end_ndex = len(draw_path)canvas = tk.Canvas(width = 500, height = 500)
canvas.pack()t = turtle.RawTurtle(canvas)
tk.Button(text = "Start", command = lambda: start(t, draw_path)).pack(side = tk.LEFT)
tk.Button(text = "Stop", command = stop).pack(side = tk.LEFT)
tk.Button(text = "Clear", command = clear).pack(side = tk.LEFT)
root.mainloop()
Answer

You cannot stop each draw statement unless you provide a checker in between each line drawn.

The below code is just a rough mock up of how you could make something to check for a tracking variable used to tell it to no longer draw new lines.

The closest thing you can do to being able to stop drawing is something like this:

import turtle
import tkinter as tkdef start():global trackertracker = Falseif tracker == False:t.forward(100)if tracker == False:t.right(90)if tracker == False:t.forward(100)if tracker == False:t.left(90)if tracker == False:t.forward(100)if tracker == False:t.right(90)if tracker == False:t.forward(100)if tracker == False:t.right(90)if tracker == False:t.forward(100)def stop():global trackertracker = Truedef clear():canvas.delete("all")root = tk.Tk()
tracker = False
canvas = tk.Canvas(width = 500, height = 500)
canvas.pack()t = turtle.RawTurtle(canvas)tk.Button(text = "Start", command = start).pack(side = tk.LEFT)
tk.Button(text = "Stop", command = stop).pack(side = tk.LEFT)
tk.Button(text = "Clear", command = clear).pack(side = tk.LEFT)root.mainloop()

This will at least stop drawing after each line but you cannot stop mid line draw.

Just for the fun of it if we add some tracking variables and use some cleaner logic we can start, stop and start again.

Update: From @cdlane's comment below I have added addition tracking and updated the clear function. This should allow for start stop start without issues and also be able to clear the field.

import turtle
import tkinter as tkdef start(turtle_object, draw_path):global tracker, start_ndex, end_ndex, startedtracker = Falseif started == False:started = Truefor i in range(start_ndex, end_ndex):if tracker == False and i <= end_ndex:pth = draw_path[i]if pth[0] == "f":turtle_object.forward(pth[1])elif pth[0] == "r":turtle_object.right(pth[1])elif pth[0] == "l":turtle_object.left(pth[1])start_ndex += 1def stop():global tracker, startedtracker = Truestarted = Falsedef clear():global t, tracker, started, start_ndexcanvas.delete("all")tracker = Falsestart_ndex = 0started = Falset = turtle.RawTurtle(canvas)root = tk.Tk()
tracker = False
start_ndex = 0
started = False # added this tracking variable to prevent issues with spamming the start button.draw_path = [["f", 100], ["r", 90], ["f", 100], ["l", 90], ["f", 100], ["r", 90], ["f", 100], ["r", 90], ["f", 100]]end_ndex = len(draw_path)canvas = tk.Canvas(width = 500, height = 500)
canvas.pack()t = turtle.RawTurtle(canvas)
tk.Button(text = "Start", command = lambda: start(t, draw_path)).pack(side = tk.LEFT)
tk.Button(text = "Stop", command = stop).pack(side = tk.LEFT)
tk.Button(text = "Clear", command = clear).pack(side = tk.LEFT)
root.mainloop()
https://en.xdnf.cn/q/118718.html

Related Q&A

binascii.Error: Incorrect padding How to decode the end with /

I received a string encoded with base64, I am using python to decode it, but decoding failed, I found that the string is followed by / ends, I dont know how to decode it, I havent found the answer, who…

How to match words in a list with user input in python?

I am working on program which takes user input and replaces the words in a list with x. eg is the word is sucks and user input is "this word is sucks". the output should be "this word i…

How to plot a ROC curve using dataframe converted from CSV file

I was trying to plot a ROC curve by using the documentation provided by sklearn. My data is in a CSV file, and it looks like this.It has two classes Goodand Badscreenshot of my CSV fileAnd my code look…

SyntaxError: Non-ASCII character. Python

Could somebody tell me which character is a non-ASCII character in the following:Columns(str) – comma-seperated list of values. Works only if format is tab or xls. For UnitprotKB, some possible column…

A pseudocode algorithm for integer addition based on binary operation

I have tried for ages to come up with a solution but just cant get my head around it.It needs to be based on two integers on the use of standard logical operations which have direct hardware implementa…

How to efficiently split overlapping ranges?

I am looking for an efficient method to split overlapping ranges, I have read many similar questions but none of them solves my problem. The problem is simple, given a list of triplets, the first two e…

pass 2D array to linear regression (sklearn)

I want to pass 2D array to linear regression: x = [[1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 3, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1],[0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0,…

How do I fix this OverflowError?

I keep getting a "OverflowError: math range error". No matter what I input, the result is the same. Im running Python 3.3, and its finding the problem at the last line. How do I fix this? (A…

Pyinstaller subprocess.check_output error

Ive bundled my app with pyinstaller to 2 *.exegui_app.exe (onefile) config.ini \libs (onedir)winservice.exe+ all DLLs and libsWhen I manually install service with command winservice.exe install everyth…

Exception handler to check if inline script for variable worked

I need to add exception handling that considers if line 7 fails because there is no intersection between the query and array brands. Im new to using exception handlers and would appreciate any advice o…