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