Turtle in Tkinter creating multiple windows

2024/10/11 18:17:48

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 turtlewindow = 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 windowdef cleanup():turtle.tracer(True)arc1.ht()arc2.ht()turtle.done()#animate the turtlesdef moveTurtles(rangevar,radius,extent,decrease):for distance in range(rangevar):arc1.circle(-radius,extent = extent)arc2.circle(-radius,extent = extent)radius -= decrease#Set the turtledef 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 canvasdef draw():#set variablesrangevar = 200radius = 200decrease = 1extent = 2#setup and draw the outlineturtle.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 programdef 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

Answer

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, TurtleScreendef 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 -= decreasedef 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 variablesrangevar = 200radius = 200decrease = 1extent = 2screen.tracer(False)  # turn off animation while drawing outline# setup and draw the outlinesetTurtle(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 logosetTurtle(arc1, 0, 200, 0, 20, 'black')setTurtle(arc2, 14, -165, 180, 20, 'black')moveTurtles(rangevar, radius, extent, decrease)# main programwindow = 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://en.xdnf.cn/q/118290.html

Related Q&A

Array tkinter Entry to Label

Hey Guys I am beginner and working on Project Linear and Binary search GUI application using Tkinter, I want to add multiple Entry boxes values to label and in an array here, I tried but its not workin…

Grid search and cross validation SVM

i am implementing svm using best parameter of grid search on 10fold cross validation and i need to understand prediction results why are different i got two accuracy results testing on training set not…

Accessing dynamically created tkinter widgets

I am trying to make a GUI where the quantity of tkinter entries is decided by the user.My Code:from tkinter import*root = Tk()def createEntries(quantity):for num in range(quantity):usrInput = Entry(roo…

Graphene-Django Filenaming Conventions

Im rebuilding a former Django REST API project as a GraphQL one. I now have queries & mutations working properly.Most of my learning came from looking at existing Graphene-Django & Graphene-Py…

Summing up CSV power plant data by technology and plant name

Ive got a question regarding the Form 860 data about US power plants.It is organized block-wise and not plant-wise. To become useful, the capacity numbers must be summed up.How may I get the total capa…

Send and receive signals from another class pyqt

I am needing a way to receive signals sent by a Class to another class. I have 2 classes: In my first class I have a function that emits a signal called asignal In my second class I call the first cla…

I can not add many values in one function

I have a gui applicationI put text into text box1, text box2,………… text box70 ,and then click on the pushButton, The function return_text () in the module_b.py be called. Now I can call one instance…

Close browser popup in Selenium Python

I am scraping a page using Selenium, Python. On opening the page one Popup appears. I want to close this popup anyway. I tried as below:url = https://shopping.rochebros.com/shop/categories/37browser = …

How can I replace certain string in a string in Python?

I am trying to write two procedures to replace matched strings in a string in python. And I have to write two procedures. def matched_case(old new): .........note: inputs are two strings, it returns a…

Python: `paste multiple (unknown) csvs together

What I am essentially looking for is the `paste command in bash, but in Python2. Suppose I have a csv file:a1,b1,c1,d1 a2,b2,c2,d2 a3,b3,c3,d3And another such:e1,f1 e2,f2 e3,f3I want to pull them toget…