Turtle freehand drawing

Question:

I’m working on a simple drawing program that combines Tkinter and Turtle modules.

I would like to add an option that the user can draw anything by just using mouse similar to pen widget on Paint. I tried many things, I could not figure out how l can do it.How can l make the turtle draw anything (like pen widget on Paint ) on canvas by using mouse

from tkinter import *
import turtle

sc=Tk()
sc.geometry("1000x1000+100+100")

fr4=Frame(sc,height=500,width=600,bd=4,bg="light green",takefocus="",relief=SUNKEN)

fr4.grid(row=2,column=2,sticky=(N,E,W,S))

#Canvas
canvas = Canvas(fr4,width=750, height=750)
canvas.pack()

#Turtle
turtle1=turtle.RawTurtle(canvas)
turtle1.color("blue")
turtle1.shape("turtle")

points=[]

spline=0

tag1="theline"

def point(event):
    canvas.create_oval(event.x, event.y, event.x+1, event.y+1, fill="red")
    points.append(event.x)
    points.append(event.y)
    return points

def canxy(event):
    print (event.x, event.y)

def graph(event):
    global theline
    canvas.create_line(points, tags="theline")

def toggle(event):
    global spline
    if spline == 0:
        canvas.itemconfigure(tag1, smooth=1)
        spline = 1
    elif spline == 1:
        canvas.itemconfigure(tag1, smooth=0)
        spline = 0
    return spline

canvas.bind("<Button-1>", point)

canvas.bind("<Button-3>", graph)

canvas.bind("<Button-2>", toggle)

sc.mainloop()
Asked By: ömer sarı

||

Answers:

The following code will let you freehand draw with the turtle. You’ll need to integrate with the rest of your code:

import tkinter
import turtle

sc = tkinter.Tk()
sc.geometry("1000x1000+100+100")

fr4 = tkinter.Frame(sc, height=500, width=600, bd=4, bg="light green", takefocus="", relief=tkinter.SUNKEN)

fr4.grid(row=2, column=2, sticky=(tkinter.N, tkinter.E, tkinter.W, tkinter.S))

# Canvas
canvas = tkinter.Canvas(fr4, width=750, height=750)
canvas.pack()

# Turtle
turtle1 = turtle.RawTurtle(canvas)
turtle1.color("blue")
turtle1.shape("turtle")

def drag_handler(x, y):
    turtle1.ondrag(None)  # disable event inside event handler
    turtle1.goto(x, y)
    turtle1.ondrag(drag_handler)  # reenable event on event handler exit

turtle1.ondrag(drag_handler)

sc.mainloop()
Answered By: cdlane