[Python] Tkinter With Turtle

Discussão em 'Python' iniciado por Stack, Janeiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I was creating a tkinter/turtle program similar to MS paint and I have the barebones Turtle finished but I am unsure of how to add the turtle into tkinter as a sort of window (Having the tkinter as the main application window with a window of python inside the window acting as a widget almost how a label or checkbox or check button would) how would I implement this given the code:

    import turtle
    import tkinter as tk
    from tkinter import ttk

    root = tk.Tk()
    root.title("")

    frame = tk.Frame(root, padx=20, pady=20)
    frame.pack(padx=10, pady=10)

    label = tk.Label(frame, text="", font=("Arial", 16, "bold"))
    label.pack()

    # Setup turtle
    t = turtle.Turtle()
    t.shape("square")
    t.fillcolor("")
    t.up() # Pen is up by default
    t.turtlesize(1000000, 1000000) # Hide the turtle border
    turtle.tracer(False)
    turtle.hideturtle()

    # Functions

    def draw(x, y):
    t.down() # Pen down to draw
    t.goto(x, y)
    turtle.update()

    def move(x, y):
    t.up() # Pen up to move without drawing
    t.goto(x, y)
    turtle.update()

    # Bind events
    t.ondrag(draw) # Draw when dragging
    turtle.onscreenclick(move) # Move when clicking # Press Space to Save
    turtle.listen()

    turtle.done()

    root.mainloop()

    Continue reading...

Compartilhe esta Página