2

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()
ggorlen
59.5k9 gold badges119 silver badges174 bronze badges
asked Jan 13, 2025 at 19:30

2 Answers 2

2

The Screen and Turtle classes are used for standalone turtle programs. When embedding in a Tk program, use TurtleScreen and RawTurtle instead:

from turtle import RawTurtle, TurtleScreen
import tkinter as tk
# Functions
def draw(x, y):
 turtle.ondrag(None)
 turtle.down() # Pen down to draw
 turtle.goto(x, y)
 turtle.ondrag(draw)
def move(x, y):
 turtle.up() # Pen up to move without drawing
 turtle.goto(x, y)
root = tk.Tk()
root.title("Title")
frame = tk.Frame(root, padx=20, pady=20)
frame.pack(padx=10, pady=10)
label = tk.Label(frame, text="Label", font=('Arial', 16, 'bold'))
label.pack()
canvas = tk.Canvas(frame, width=400, height=300)
canvas.pack()
screen = TurtleScreen(canvas)
turtle = RawTurtle(screen)
turtle.shape('square')
turtle.fillcolor(screen.bgcolor())
turtle.penup() # Pen is up by default
# Bind events
turtle.ondrag(draw) # Draw when dragging
screen.onclick(move) # Move when clicking
screen.mainloop()

Review TurtleScreen and RawTurtle in the Python turtle documentation

answered Jan 14, 2025 at 22:07
Sign up to request clarification or add additional context in comments.

Comments

-1

You can use the RawTurtle() function to define your turtle then from there you can use ScrolledCanvas() function and TurtleScreen to create the screen and then it goes into tkinter, then you can use screen instead of updating turtle itself.

from turtle import RawTurtle, TurtleScreen, ScrolledCanvas
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Drawing in turtle with TKinter")
# Create a ScrolledCanvas for the turtle
canvas = ScrolledCanvas(root, width=800, height=600) # 
Canvas with scrollbars
canvas.pack(fill=tk.BOTH, expand=True) # Make the canvas 
 expand to fill the window
# Create a TurtleScreen from the canvas
 screen = TurtleScreen(canvas) # TurtleScreen is needed for 
 turtle graphics
screen.bgcolor("white") # Set the background color of the screen
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 = RawTurtle(screen) # Create a turtle object
t.shape("square") # Set the shape of the turtle
t.fillcolor("") # No fill color for the turtle
t.pencolor("black") # Set the default pen color
t.pensize(10)
t.up() # Pen is up by default (no drawing when moving)
t.turtlesize(1000000, 1000000) # Hide the turtle border by 
making it extremely large
screen.tracer(False) # Disable automatic screen updates for 
smoother drawing
# Functions
def draw(x, y):
 t.down() # Pen down to draw
 t.goto(x, y)
 screen.update()
def move(x, y):
 t.up() # Pen up to move without drawing
 t.goto(x, y)
 screen.update()
# Bind events
t.ondrag(draw) # Draw when dragging
screen.onscreenclick(move) # Move when clicking # Press 
Space to Save
root.mainloop()
answered Jan 15, 2025 at 19:19

1 Comment

I suggest fixing the syntax errors in this answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.