Synt4xErr0r 7
Synt4xErr0r 7

Reputation: 35

Is it possible to use turtle objects in a tkinter screen?

I wanted to make a sort of tennis game and I decided to use Tkinter, although I hadn't figured out how to add shapes or images to the screen. is there a way to use the turtle module to add objects using "object = "t.Turtle()" onto the tkinter screen?

Upvotes: 0

Views: 915

Answers (2)

cdlane
cdlane

Reputation: 41872

Turtle has two models under which it operates, standalone and embedded (i.e. embedded in tkinter). Rather than poke around in turtle's underpinnings to make standalone turtle play with tkinter, we can put tkinter in charge and call up turtles as needed. Here's the first example provided by @furas redone in this manner:

import tkinter
from turtle import RawTurtle

# --- functions ---

def move():
    turtle.left(35)
    turtle.forward(50)

# --- main ---

root = tkinter.Tk()

tkinter.Button(root, text="Before Canvas", command=move).pack()

canvas = tkinter.Canvas(root)

canvas.pack()

tkinter.Button(root, text="After Canvas", command=move).pack()

turtle = RawTurtle(canvas)

root.mainloop()

Along with RawTurtle, the other embedded API objects of interest are TurtleScreen and ScrolledCanvas if you need more of standalone turtle's window features associated with the tkinter canvas upon which turtle crawls.

Upvotes: 1

furas
furas

Reputation: 142651

turtle already uses tkinter to display window and tkinter.Canvas() to draw in window.

I don't know if there is method to add existing turtle to new tkinter window but there is method to access already existing turtle window and use some tkinter functions - ie. add tkinter.Buttons


This example adds one button above canvas, and one button below canvas.

import tkinter
import turtle

# --- functions ---

def move():
    turtle.left(30)
    turtle.forward(50)
    
# --- main ---

canvas = turtle.getcanvas()
root = canvas.master

button1 = tkinter.Button(root, text="Before Canvas", command=move)
button1.pack(before=canvas)

button2 = tkinter.Button(root, text="After Canvas", command=move)
button2.pack() # .pack(after=canvas)

turtle.mainloop()

enter image description here


And this example puts button on canvas

import tkinter
import turtle

# --- functions ---

def move():
    turtle.left(30)
    turtle.forward(50)
    
# --- main ---

canvas = turtle.getcanvas()
root = canvas.master

root.geometry('500x500')

button = tkinter.Button(root, text="On Canvas", command=move)
canvas.create_window(0, 0, window=button)

turtle.mainloop()

enter image description here


You can also use other canvas functions to draw figures canvas.create_rectangle(), canvas.create_image(), etc. See old effbot's documentation for Canvas on web.archive.org

import tkinter
import turtle
from PIL import ImageTk

# --- functions ---

# --- main ---

canvas = turtle.getcanvas()
root = canvas.master

root.geometry('500x500')

# for some file formats it can work - ie. png, gif
#photo = tkinter.PhotoImage(file='images/lenna.png')

# for other file formats it needs `pillow` - ie. jpg, tiff, webp
photo = ImageTk.PhotoImage(file='images/lenna.jpg')

canvas.create_image(0, 0, image=photo, anchor='center')

canvas.create_rectangle(-50, -50, 50, 50, fill='red')
canvas.create_text(0, 0, text='HELLO')

#for _ in range(12):
#    turtle.left(30)
#    turtle.forward(50)

turtle.mainloop()

enter image description here

Image Lenna from Wikipedia


EDIT:

Version which adds turtle to existing tkinter window.

It uses 3 different canvas and I expect they may have different functionality - but you would have to read turtle documentation for details.

import tkinter as tk
import turtle

root = tk.Tk()

l = tk.Label(root, text='Turtle in Tkinter')
l.pack()

# ---

s1 = tk.Canvas(root)
s1.pack()

# ---

s2 = turtle.ScrolledCanvas(root)
s2.pack()

# --- 
s3 = tk.Canvas(root)
s3.pack()
s3t = turtle.TurtleScreen(s3)

# ---

b = tk.Button(root, text='exit', command=root.destroy)
b.pack()

# ---

t1 = turtle.RawTurtle(s1)

for _ in range(12):
    t1.left(30)
    t1.forward(50)

t2 = turtle.RawTurtle(s2)

for _ in range(12):
    t2.right(30)
    t2.forward(50)

t3 = turtle.RawTurtle(s3t)

for _ in range(12):
    t3.left(30)
    t3.forward(50)
    
root.mainloop()

enter image description here

Upvotes: 1

Related Questions