Запись текста на экране черепахи по своей сути не переплетается с onpresskey
. Вы можете просто записать текст на экран, написать текст при нажатии клавиши, написать текст при нажатии кнопки мыши и т. Д. Вот пример кода, который выполняет все три действия:
from turtle import Turtle, Screen
FONT = ("Arial", 14, "bold")
class Graphics:
def __init__(self):
self.screen = Screen()
self.screen.setup(width=1280, height=800)
self.screen.listen()
self.turtle = Turtle(visible=False)
self.turtle.speed('fastest')
def text_at_xy(self, x, y, text):
self.turtle.penup()
self.turtle.goto(x, y)
self.turtle.write(text, font=FONT)
def text_onkey(self, x, y, text, key):
self.screen.onkey(lambda x=x, y=y, text=text: self.text_at_xy(x, y, text), key)
def text_onmouseclick(self, text):
self.screen.onclick(lambda x, y, text=text: self.text_at_xy(x, y, text))
graphics = Graphics()
graphics.text_at_xy(100, 100, "Static Text") # just print text at location
graphics.text_onkey(-100, -100, "On Key Text", "j") # print text at location when you type "j"
graphics.text_onmouseclick("On Mouse Click Text") # print text whereever mouse is clicked
graphics.screen.mainloop()
Основываясь на работе @ martineau и комментариях @ DanD, мы могли бы печатать непрерывно, выполнив:
from turtle import Turtle, Screen
FONT = ("Arial", 14, "bold")
class Graphics:
def __init__(self):
self.screen = Screen()
self.screen.setup(width=1280, height=800)
self.screen.listen()
self.turtle = Turtle(visible=False)
self.turtle.speed('fastest')
self.screen.cv.bind("<KeyPress>", lambda event: self.text_xy(event.char))
def text_xy(self, char):
self.turtle.penup()
self.turtle.write(char, move=True, font=FONT)
graphics = Graphics()
graphics.screen.mainloop()
Обратите внимание, что для реализации этого мы опускаемся ниже API-интерфейса turtle в основу tkinter.