Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrate turtle module with tkinter canvas [closed]

I am trying to integrate the Turtle module into an interface I have created with TKInter, currently I have a canvas where I would like for the turtle to draw to (see example 1). However I am lost in how to get the draw to it.

Example

like image 546
E J Avatar asked Dec 11 '22 11:12

E J


1 Answers

Try this:

import turtle
import tkinter as tk

def forward():
    t.forward(100)

def back():
    t.back(100)

def left():
    t.left(90)

def right():
    t.right(90)

root = tk.Tk()
canvas = tk.Canvas(master = root, width = 500, height = 500)
canvas.pack()

t = turtle.RawTurtle(canvas)
t.pencolor("#ff0000") # Red

t.penup()   # Regarding one of the comments
t.pendown() # Regarding one of the comments

tk.Button(master = root, text = "Forward", command = forward).pack(side = tk.LEFT)
tk.Button(master = root, text = "Back", command = back).pack(side = tk.LEFT)
tk.Button(master = root, text = "Left", command = left).pack(side = tk.LEFT)
tk.Button(master = root, text = "Right", command = right).pack(side = tk.LEFT)

root.mainloop()

I have never used this module before but what I have written seems to do what you want.

References:

  • http://www.eg.bucknell.edu/~hyde/Python3/TurtleDirections.html
  • https://www.reddit.com/r/learnpython/comments/4qdcmw/can_you_add_turtle_graphics_to_a_tkinter_window/
like image 110
Joshua Nixon Avatar answered Dec 13 '22 01:12

Joshua Nixon