Tkinter中的“停止”按钮

时间:2018-11-15 18:46:17

标签: python tkinter turtle-graphics tkinter-canvas

我正在尝试让乌龟动画从一个按钮开始,再从一个按钮停止。从一个按钮开始很容易,但是我似乎无法找出一个停止按钮?到目前为止,这是我的代码:

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.stop

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

清除按钮也起作用,但是之后开始按钮不再起作用。如果有人也可以帮助我。

感谢@Mike-SMT帮助我使用此代码。这是经过编辑且功能齐全的代码:

import turtle
import tkinter as tk


def start(turtle_object, draw_path):
    global tracker, start_ndex, end_ndex, started
    tracker = False
    if started == False:
        started = True
        for 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 += 1


running = True

def stop():
    global tracker, started
    tracker = True
    started = False

def clear():
    global t, tracker, started, start_ndex
    t.reset()
    start_ndex = 0
    started = False
    t = 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()

2 个答案:

答案 0 :(得分:0)

除非在绘制的每行之间都提供一个检查器,否则您不能停止每个绘制语句。

下面的代码只是一个粗略的模型,它说明了如何制作一些东西来检查用于指示跟踪变量不再绘制新行的跟踪变量。

与停止绘制最接近的事情是这样的:

import turtle
import tkinter as tk

def start():
    global tracker
    tracker = False
    if 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 tracker
    tracker = True

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

这至少会在每行之后停止绘制,但是您不能停止中线绘制。

只是有趣的是,如果我们添加一些跟踪变量并使用更简洁的逻辑,我们就可以启动,停止和重新启动。

更新:从下面@cdlane的评论中,我添加了附加跟踪并更新了清除功能。这应该允许启动停止启动而没有问题,并且还可以清除该字段。

import turtle
import tkinter as tk

def start(turtle_object, draw_path):
    global tracker, start_ndex, end_ndex, started
    tracker = False
    if started == False:
        started = True
        for 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 += 1

def stop():
    global tracker, started
    tracker = True
    started = False

def clear():
    global t, tracker, started, start_ndex
    canvas.delete("all")
    tracker = False
    start_ndex = 0
    started = False
    t = 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()

答案 1 :(得分:0)

我同意@ Mike-SMT的回答中的想法,但我会以不同的方式去做。在我看来,乌龟及其控件非常像一个Python生成器,因此我已经将其重铸。乌龟沿着路径移动,一次移动一个像素,从而产生可能会或可能不会返回的控制。或者它可能耗尽其路径并停止迭代:

import logging

def main():
    log = logging.getLogger()
    hdlr = logging.FileHandler(r'C:\Users\kjain\Desktop\!exports\bug\errors.log', mode='w')
    formatter = logging.Formatter('''%(asctime)s\n%(levelname)s: %(filename)s | %(funcName)s | Line: %(lineno)d
%(message)s\n''')
    hdlr.setFormatter(formatter)
    log.addHandler(hdlr)

    try:
        callDiv()
    except Exception as e:
        log.error(e)

def callDiv():
    raise Exception('My error message')

main()
  

清除按钮也可以使用,但是之后开始按钮不可用   工作了。如果有人也可以帮助我。

您可以将当前的callDiv函数替换为:

import tkinter as tk
from turtle import RawTurtle

PATH = [(100.00, 0.00), (100.00, -100.00), (200.00, -100.00), (200.00, -200.00), (100.00, -200.00)]

def run():
    for position in PATH:
        turtle.setheading(turtle.towards(position))

        while turtle.distance(position) > 1:
            turtle.forward(1)
            yield

def start():
    global generator, running

    running = True

    while running:
        try:
            next(generator)

        except ValueError:  # user clicked start but already running
            return

        except TypeError:  # new run
            turtle.reset()
            generator = run()

        except StopIteration:  # end of complete run
            generator = None
            running = False
            break

def stop():
    global running
    running = False

def clear():
    global generator
    turtle.reset()
    generator = None

root = tk.Tk()
canvas = tk.Canvas(width=500, height=500)
canvas.pack()

turtle = RawTurtle(canvas, "turtle")

running = True
generator = None

tk.Button(text="Start", command=start).pack(side=tk.LEFT, expand=tk.TRUE)
tk.Button(text="Stop", command=stop).pack(side=tk.LEFT, expand=tk.TRUE)
tk.Button(text="Clear", command=clear).pack(side=tk.LEFT, expand=tk.TRUE)

root.mainloop()

如果您只想删除绘制的路径,但将乌龟留在最终的位置。如果要删除路径并将海龟重置回起点,请执行以下操作:

clear()