Turtle in Tkinter creating multiple windows

倾然丶 夕夏残阳落幕 提交于 2021-01-29 09:05:19

问题


I am attempting to create a quick turtle display using Tkinter, but some odd things are happening.

First two turtle windows are being created, (one blank, one with the turtles), secondly, any attempt of turning the tracer off is not working.

This might be a simple fix but at the moment I cannot find it.

Any help would be appreciated,

Below is the code:

import tkinter as tk
import turtle

window = tk.Tk()
window.title('Top 10\'s')

def loadingscreen():

    canvas = tk.Canvas(master = window, width = 500, height = 500)
    canvas.pack()

    arc1 = turtle.RawTurtle(canvas)
    arc2 = turtle.RawTurtle(canvas)


    #clean up the turtles and release the window
    def cleanup():
        turtle.tracer(True)
        arc1.ht()
        arc2.ht()
        turtle.done()

    #animate the turtles
    def moveTurtles(rangevar,radius,extent,decrease):
        for distance in range(rangevar):
           arc1.circle(-radius,extent = extent)
           arc2.circle(-radius,extent = extent)
           radius -= decrease

    #Set the turtle
    def setTurtle(turt,x,y,heading,pensize,color):
       turt.pu()
       turt.goto(x,y)
       turt.pd()
       turt.seth(heading)
       turt.pensize(pensize)
       turt.pencolor(color)

    #draw on the canvas
    def draw():
        #set variables
        rangevar = 200
        radius = 200
        decrease = 1
        extent = 2

        #setup and draw the outline
        turtle.tracer(False)
        setTurtle(arc1,0,200,0,40,'grey')
        setTurtle(arc2,14,-165,180,40,'grey')
        moveTurtles(rangevar,radius,extent,decrease)

        #setup and animate the logo 
        turtle.tracer(True)
        setTurtle(arc1,0,200,0,20,'black')
        setTurtle(arc2,14,-165,180,20,'black')
        moveTurtles(rangevar,radius,extent,decrease)

    #main program
    def main():
        turtle.tracer(False)
        arc1.speed(0)
        arc2.speed(0)
        draw()
        cleanup()

    if __name__ == "__main__":
        try:
            main()
        except:
            print("An error occurred!!")



loadingscreen()

Essentially I am creating a Tk window, then a canvas, then two turtles, and then animating these turtles


回答1:


My guess is you're trying to call turtle screen methods without actually having a turtle screen. When turtle is embedded in tkinter like this, you can overlay a Canvas with a TurtleScreen instance which will provide some, but not all, of the screen features of the standalone turtle:

import tkinter as tk
from turtle import RawTurtle, TurtleScreen

def cleanup():
    """ hide the turtles """

    arc1.hideturtle()
    arc2.hideturtle()

def moveTurtles(rangevar, radius, extent, decrease):
    """ animate the turtles """

    for _ in range(rangevar):
        arc1.circle(-radius, extent=extent)
        arc2.circle(-radius, extent=extent)
        radius -= decrease

def setTurtle(turtle, x, y, heading, pensize, color):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

    turtle.setheading(heading)
    turtle.pensize(pensize)
    turtle.pencolor(color)

def draw():
    # set variables
    rangevar = 200
    radius = 200
    decrease = 1
    extent = 2

    screen.tracer(False)  # turn off animation while drawing outline

    # setup and draw the outline
    setTurtle(arc1, 0, 200, 0, 40, 'grey')
    setTurtle(arc2, 14, -165, 180, 40, 'grey')
    moveTurtles(rangevar, radius, extent, decrease)

    screen.tracer(True)  # turn animation back on for the following

    # setup and animate the logo
    setTurtle(arc1, 0, 200, 0, 20, 'black')
    setTurtle(arc2, 14, -165, 180, 20, 'black')
    moveTurtles(rangevar, radius, extent, decrease)

# main program

window = tk.Tk()
window.title("Top 10's")

canvas = tk.Canvas(master=window, width=500, height=500)
canvas.pack()

screen = TurtleScreen(canvas)

arc1 = RawTurtle(screen)
arc1.speed('fastest')

arc2 = RawTurtle(screen)
arc2.speed('fastest')

draw()
cleanup()

Another suggestion: don't mess with tracer() until after everything else is working and then (re)read it's documentation carefully.



来源:https://stackoverflow.com/questions/52452857/turtle-in-tkinter-creating-multiple-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!