Python Turtle key bindings only working once

I was trying to creating a simple keyboard drawing program with turtle that saves your drawing for later, however when trying to use the key bindings to move around, I can only move once.

I am running Windows 10. I am writing this for a rendering engine I built called Ren that uses this file-format to render shapes.

Here is my code:

import turtle
from tkinter.filedialog import *

s = turtle.Screen()
s.title("Ren Poly Editor")

t = turtle.Turtle()

fileName = asksaveasfilename()
with open(fileName, "w") as file:
    file.write("")

def fd():
    distance = s.textinput("Forward", "How far?")

    t.fd(int(distance))

    with open(fileName, "a") as file:
        file.write("fd " + distance)

def bk():
    distance = s.textinput("Backward", "How far?")

    t.back(int(distance))

    with open(fileName, "a") as file:
        file.write("bk " + distance)

def lt():
    distance = s.textinput("Forward", "How much?")

    t.lt(int(distance))

    with open(fileName, "a") as file:
        file.write("lt " + distance)

def rt():
    distance = s.textinput("Forward", "How much?")

    t.rt(int(distance))

    with open(fileName, "a") as file:
        file.write("rt " + distance)


s.onkeypress(fd, "Up")
s.onkeypress(bk, "Down")
s.onkeypress(lt, "Left")
s.onkeypress(rt, "Right")
s.listen()

s.mainloop()

Source: Python Questions

LEAVE A COMMENT