Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I use the tkinter module to make a controller for turtle?

I want to make a controller for the turtle module using the tkinter module. I wrote a code myself but it didn’t work. It also contains four forward, backward, left and right.

Can someone answer me and explain with a solution?

from tkinter import *
import turtle

def turtle():
    if Button1==1:
        turtle.forward(100)
        done()
    if Button2==1:
        turtle.backward(100)
        done()
    if button3==1:
        turtle.left(90)
        done()
    if button4==1:
        turtle.right(90)
        done()
    else:
        done()



window = TK()
window.title("turtle")
window.minsize(1000,700)


Button1(window,text="forward",command=turtle).pack()
Button2(window,text="backward",command=turtle).pack()   
Button3(window,text="left",command=turtle).pack()   
Button4(window,text="right",command=turtle).pack()
Window.mainloop()
turtle.done() 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Problems I see in your code include: Button1 through Button4 don’t exist as object classes; you sometimes use window and sometimes use Window; using both window.mainloop() and turtle.done() is redundant, pick one; the done() function isn’t defined, nor needed; you’re invoking standalone turtle but inside a tkinter program you should use embedded turtle (i.e. RawTurtle); you misspell Tk() as TK().

How I might go about writing the basic code:

from tkinter import *
from turtle import RawTurtle
from functools import partial

 window = Tk()
 window.title("turtle")

 canvas = Canvas(window)
 canvas.pack()

 turtle = RawTurtle(canvas)

 Button(window, text="forward", command=partial(turtle.forward, 100)).pack()
 Button(window, text="backward", command=partial(turtle.backward, 100)).pack()
 Button(window, text="left", command=partial(turtle.left, 90)).pack()
 Button(window, text="right", command=partial(turtle.right, 90)).pack()

 window.mainloop()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading