pythonbuttonturtle-graphicspython-turtle

How can I create a button in turtle?


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.


Solution

  • 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()