First, I import the turtle module and then creating a screen. After that, I try to create a turtle object, but it is not showed on the screen. Could anyone tell me what the issue is? I have written the following code in PyCharm:
import turtle as t
screen = t.Screen()
screen.title("My Snake Game")
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.exitonclick()
terry = t.Turtle(shape='square')
terry.color('white')
terry.goto(0, 0)
The screen.exitonclick() method does not only set the property that the screen will close after clicking it, it does also wait for this to happen. As this method waits for the screen to close, every code that is after it won't be executed untill the user closes the screen by clicking on it.
This means that the turtle will be created after the screen is closed and thus does not show up as the screen is closed. The solution for this is moving the screen.exitonclick() line to the end of the code.
screen.exitonclick() seems to wait for a mouse click before continuing. As you define your turtle after that, it doesn't come up on the screen. Try putting screen.exitonclick() at the very end of the code
The problem here is that you have the screen.exitonclick() in the middle. That generates another window that apears and desapears after screen.exitonclick() is executed. What you should do is to put screen.exitonclick() at the end.
import turtle as t
screen = t.Screen()
screen.title("My Snake Game")
screen.setup(width=600, height=600)
screen.bgcolor("black")
terry = t.Turtle(shape='square')
terry.color('white')
terry.goto(0, 0)
screen.exitonclick()
This would be the code.
This is for a school project - I am still a beginner and have lots of trouble with functions. How would I clear the canvas with a click?
The answer depends on how severe a clearing you want to do. If you simply want to remove the drawing done by an individual turtle, but leave drawings by other turtles (background) in tact, as well as preserve aspects of the screen like its background color, you can use the screen's onclick() event to invoke an individual turtle's clear() method:
from turtle import Screen, Turtle
def clear_turtle(x, y):
turtle.clear()
screen = Screen()
screen.bgcolor('lavender')
screen.onclick(clear_turtle)
yertle = Turtle()
yertle.hideturtle()
yertle.dot(100)
turtle = Turtle()
turtle.hideturtle()
turtle.dot(50, 'yellow')
screen.mainloop()
If, on the other hand, you want a more scorched earth result, you can use the screen's onclick() event to invoke the screen's clear() method:
from turtle import Screen, Turtle
def clear_screen(x, y):
screen.clear()
screen = Screen()
screen.bgcolor('lavender')
screen.onclick(clear_screen)
yertle = Turtle()
yertle.hideturtle()
yertle.dot(100)
turtle = Turtle()
turtle.hideturtle()
turtle.dot(50, 'yellow')
screen.mainloop()
This will return the screen to it's initial blank (white) state and destroy all your turtle drawings as well as your turtles all the way down.
This is the code. It is indeed right and I tried the simplest way just to see if a dialog box appears, but still doesn't show up.
from turtle import Turtle, Screen
tim = Turtle()
screen = Screen()
screen.setup(width=500, height=400)
tur_name = screen.textinput(title="Who will win?", prompt="Enter the turtle color:")
screen.exitonclick()
How to create a simple button in turtle, python, where if you click it, you can define it to print messages, or do other, more complex things.
You can embed turtle in tkinter, as #JoshuaNixon suggests in his comment, using tkinter buttons to control your turtle canvas. If you want to work within standalone turtle, I recommend using a turtle as a button as they can be coerced into any shape and/or color and have individual onclick event handlers so you don't have to figure out where the user clicked on the screen:
from turtle import Screen, Turtle
CURSOR_SIZE = 20
FONT_SIZE = 12
FONT = ('Arial', FONT_SIZE, 'bold')
def draw_onclick(x, y):
turtle.dot(100, 'cyan')
button = Turtle()
button.hideturtle()
button.shape('circle')
button.fillcolor('red')
button.penup()
button.goto(150, 150)
button.write("Click me!", align='center', font=FONT)
button.sety(150 + CURSOR_SIZE + FONT_SIZE)
button.onclick(draw_onclick)
button.showturtle()
turtle = Turtle()
turtle.hideturtle()
screen = Screen()
screen.mainloop()
Note that Turtle.onclick() is different than Screen().onclick -- one only happens when clicking on a specific turtle instance whereas the other happens when clicking anywhere on the screen.
Since the Python Turtle Graphics Module is built on top of Tkinter, a Tkinter button should work on a turtle screen
from turtle import Screen
from tkinter import *
screen = Screen()
screen.setup(width=600, height=400)
def do_something():
print("Good bye")
canvas = screen.getcanvas()
button = Button(canvas.master, text="Exit", command=do_something)
button.pack()
button.place(x=300, y=100) # place the button anywhere on the screen
screen.exitonclick()
I haven't tried this out but this might work:
root = turtle.Screen()._root
btn = Button(root, text="This button exists in turtle")
btn.pack()
That should be it!
Note: Since turtle is based on tkinter the turtle.Screen() contains the tk() root
We are able to access that root and create a tkinter button and add it to it.
Edit: If you add a command parameter in pack you can make the button execute a function
To create a simple button, there might be other ways, but this is how I do it.
import turtle
def button(x,y):
if x < 50 and x > -50 and y < 50 and y > -50:
print(f"Your coordinates are: ({x}, {y}).")
turtle.onscreenclick(button, 1, add=False)
turtle.done()
To explain this, button is just a function, it has nothing to do with an actual button yet. The if statement in there basically takes the x,y variables that are its parameters, and checks whether they are between two numbers, in this case, coordinates.
The onscreenclick function takes three parameters. The first is a function with two parameters. Wherever you click on the turtle pop-up, it will take the x,y coordinates of where you clicked and inserts it into the function. The second is a number. This number refers to how you are going to click it (Ex. right-click, left-click, etc.) In most cases, it is 1 since 1 is left-click. Finally, the third parameter is necessary when you have multiple buttons. If you are creating a second, third, etc. button, and you want to create the new button without overwriting the previous button, you write add=True. If you want to make it so all previous buttons are canceled, you write True. So, finally, the code above would print the coordinates of where you clicked if they were both between -50 and 50.
You can do a lot of useful things with this function. You can create it as a temporary button to help you while writing with turtle, where the "whole screen" is a large button where it prints the x,y coordinates of where you clicked. This can be useful in getting the approximate coordinates of where you want your turtle to go next.
Or you could use it your actual code, to get information from the user or as part of a game.
All in all, this is a simple way to create a button just using turtle and no other modules and has great flexibility.
If there are any other ways, using or not using turtle, complex or simple, please post it below as an answer.
NOTE: You wouldn't be able to "see" the button by default. If you wanted, though, you could make a turtle draw the outline of the button or something.
I wrote a Python 3.7 program with Turtle Graphics. It is a simple program and works fine, but I want it to run full-screen when I initiate the program. How can I do this? There is no full-screen option in the Turtle documentation.
import turtle
from turtle import *
a = turtle.Turtle()
a.speed(10)
a.color('red', 'blue')
# a.begin_fill()
for i in range(90):
a.fd(200)
a.lt(169)
# a.end_fill()
turtle.done()
The width and height arguments to the setup() method take integer (pixel) and floating point (percentage of screen). By supplying 1.0 you'll get the largest window that turtle can make:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(width=1.0, height=1.0)
a = Turtle()
a.speed('fastest')
a.color('red', 'blue')
# a.begin_fill()
for _ in range(36):
a.forward(200)
a.left(170)
# a.end_fill()
screen.mainloop()
But this isn't necessarily the operating systems sense of Full Screen where the window overlays everything. It's just the largest window turtle can create given available screen real estate.