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.
-
1Intergrate Turtle with Tkinter – Joshua Nixon Jan 24 '20 at 19:51
-
Does this answer your question? [How to create a button with python turtle](https://stackoverflow.com/questions/61765658/how-to-create-a-button-with-python-turtle) – ggorlen Jan 18 '23 at 00:11
4 Answers
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.

- 40,441
- 5
- 32
- 81
-
How would you change the size of the button, like, if you wanted it to be really large? I am pretty sure that in this code the turtle is the button...correct me if I'm wrong – Mike Smith Jan 26 '20 at 22:42
-
@BobRobert, yes a turtle *is* the button. Lookup turtle's `shapesize()` method on how to enlarge, shrink, and, to some extent, distort the turtle. You can also define your own polygon for a turtle, making any shape or size button you want. – cdlane Jan 27 '20 at 04:10
-
Ok, in your method it might be easier to move the button around create different shapes (that are polygons or predefined shapes), but in mine, you can create almost any polygon, shape, etc. – Mike Smith Jan 27 '20 at 18:44
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

- 30,962
- 25
- 85
- 135

- 111
- 1
- 3
- 9
-
Or you can create a tkinter window and add a turtle to it or something – deateaterOG Sep 28 '21 at 04:00
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()

- 36
- 4
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.

- 527
- 2
- 6
- 20
-
1Your `print()` statement isn't valid as you're concatenating strings and floats. Instead try, `print("Your coordinates are:", (x, y))`. Your `turtle.listen()` isn't needed as it's for keyboard events, not mouse events. When is `y < 50 and y > 100` ever true? Finally, I can't see your button to click on it. – cdlane Jan 25 '20 at 06:20
-
Yeah, you would have to create the button outline or whatever. I guess you could create a quick simple function to create a rectangle wherever the button can be clicked, but it isn't automatically created. I have fixed the other things you mentioned as well. – Mike Smith Jan 26 '20 at 22:38