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

Time:12-30

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() 

CodePudding user response:

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()
  • Related