1

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()
cdlane
  • 40,441
  • 5
  • 32
  • 81
ömer sarı
  • 639
  • 1
  • 10
  • 30

1 Answers1

5

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()
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • thank you very much. This is exactly what l was looking for. – ömer sarı Sep 01 '16 at 21:03
  • Thank You. Very useful – Dhiraj Gandhi Apr 26 '20 at 18:20
  • Is there some reason to use `sticky=(tkinter.N, tkinter.E, tkinter.W, tkinter.S)` rather than just `sticky=('n', 'e', 'w', 's')`? Or maybe even `sticky='news'` if that would work? – Wildcard Jun 11 '20 at 23:12
  • 1
    @Wildcard, I believe tkinter will accept all those formats. – cdlane Jun 12 '20 at 02:51
  • @cdlane thanks! On a slightly related note, I've been reading through your answers about the turtle module and finding them *very* helpful. (Thanks for writing them!) I just asked a question you may be able to answer: https://stackoverflow.com/q/62336296/5419599 – Wildcard Jun 12 '20 at 02:58