python-turtle

Using screen.onclick() in Turtle with a function that returns multiple values

筅森魡賤 提交于 2021-01-29 07:08:24
问题 I am trying to use the coordinates from a user's mouse click as the x and y for another function. When that second function runs, it returns two values (not x and y). But when I run the onclick method, I get "TypeError: cannot unpack non-iterable NoneType object" before I even click anything. def on_click(x, y): t = turtle.Turtle() screen = turtle.Screen() t.penup() t.goto(x, y) #do stuff part 1 #do stuff part 2 return value1, value2 def main(): t = turtle.Turtle() screen = turtle.Screen()

How would I undo something in the python turtle module with a key press?

南笙酒味 提交于 2021-01-29 06:12:45
问题 How would I undo something in the python turtle module? Here is my code: turtle.listen() turtle.onkey(undo, "ctrl + z") # You can't combine ctrl + z, so how do I do this? 回答1: turtle is built on top of tkinter and with the latter, a control-z can be specified with "<Control-KeyPress-z>". As stated by @martineau (+1) but with a minor correction of 'H' -> 'z'. You can't do it with the functions that turtle provides. Python turtle doesn't pass key press symbols directly onto tkinter, it appends

How can I make a turtle do something when it gets close to another turtle?

独自空忆成欢 提交于 2021-01-28 20:32:54
问题 Good afternoon, I am simulating a virus outbreak with the use of turtles. I have come up with the following code, my question will be after the code: import turtle import random import time def make_population(amount): """ Creates a list representing a population with a certain amount of people. """ population = [] for person in range(amount): population.append(turtle.Turtle()) for person in population: person.shape("circle") person.shapesize(0.2) return population def random_move(person): ""

Is there a way to remove a turtle from the screen?

喜夏-厌秋 提交于 2020-07-09 12:01:25
问题 I have the following code: answer = "ABC" flag.goto(-999, -999) while (answer.lower != 'y' or answer.lower != 'n'): print("You got the flag! Free play(y/n)?") answer = input("") if answer.lower == 'y': pass if answer.lower == 'n': return None I am trying to remove the turtle called flag , through adding it to a list then deleting it with del(testlist[0]) , but it didn't work. The output is: You got the flag! Free play(y/n)? y You got the flag! Free play(y/n)? n You got the flag! Free play(y/n

My text in my clock python is not aligning properly

不羁岁月 提交于 2020-06-27 04:08:06
问题 My text in my turtle module is not aligning properly, it is aligned up and to the left. I want it to align exactly where the turtle is. Can anyone help? I tried setting the xcor and ycor of the turtle up and to the left by 5 units and that did not work. Any help would be greatly appreciated. Code: import time from datetime import datetime,date import turtle t = turtle.Pen() while True: turtle.tracer(0, 0) hour_hand = float(datetime.today().hour) minute_hand = float(datetime.today().minute)

How can I save a Turtle canvas as an image (.png or .jpg) in Python 3

折月煮酒 提交于 2020-06-17 09:37:07
问题 I've been working with the turtle module and want to use it as a starting point to work on a simple image recognition program that could recognize numbers/letters. I need to be able to save the turtle as an image I could manipulate- recaling, rotating, etc. to try to regulate the images. I've been researching for hours and cannot seem to find anything that works. I have discovered how to save the Turtle output as a Tkinter canvas: import turtle t = turtle.Turtle() # Draw something canvas = t