Python Turtle 键绑定只能工作一次
Python Turtle key bindings only working once
我试图用 turtle 创建一个简单的键盘绘图程序,以保存您的绘图供以后使用,但是当尝试使用键绑定四处移动时,我只能移动一次。
我是 运行 Windows 10。我正在为我构建的名为 Ren
的渲染引擎编写此代码,该引擎使用此文件格式来渲染形状。
这是我的代码:
import turtle
from tkinter.filedialog import *
s = turtle.Screen()
s.title("Ren Poly Editor")
t = turtle.Turtle()
fileName = askopenfilename()
with open(fileName) as file:
rendataLayer1 = file.read().split("\n")
rendata = []
for i in rendataLayer1:
if not i.startswith("#") and i:
rendata.append(str(i.split()))
t.color("black")
for i in rendata:
i = eval(i)
print(i[0] + " " + i[1])
if i[0] == "cl":
t.color(i[1])
elif i[0] == "fd":
t.fd(int(i[1]))
elif i[0] == "bk":
t.back(int(i[1]))
elif i[0] == "lt":
t.lt(int(i[1]))
elif i[0] == "rt":
t.rt(int(i[1]))
def fd():
distance = s.textinput("Forward", "How far?")
t.fd(int(distance))
with open(fileName, "a") as file:
file.write("fd " + distance + "\n")
def bk():
distance = s.textinput("Backward", "How far?")
t.back(int(distance))
with open(fileName, "a") as file:
file.write("bk " + distance + "\n")
def lt():
distance = s.textinput("Forward", "How much?")
t.lt(int(distance))
with open(fileName, "a") as file:
file.write("lt " + distance + "\n")
def rt():
distance = s.textinput("Forward", "How much?")
t.rt(int(distance))
with open(fileName, "a") as file:
file.write("rt " + distance + "\n")
s.onkeypress(fd, "Up")
s.onkeypress(bk, "Down")
s.onkeypress(lt, "Left")
s.onkeypress(rt, "Right")
s.listen()
s.mainloop()
每当您在 Python 乌龟(或类似的 numinput()
)中调用 textinput()
方法时,它都会撤消您的 listen()
调用,因为 window弹出接受输入成为监听器。只需在每次调用 textinput()
后添加另一个 listen()
调用,事情就会如您所愿。
我试图用 turtle 创建一个简单的键盘绘图程序,以保存您的绘图供以后使用,但是当尝试使用键绑定四处移动时,我只能移动一次。
我是 运行 Windows 10。我正在为我构建的名为 Ren
的渲染引擎编写此代码,该引擎使用此文件格式来渲染形状。
这是我的代码:
import turtle
from tkinter.filedialog import *
s = turtle.Screen()
s.title("Ren Poly Editor")
t = turtle.Turtle()
fileName = askopenfilename()
with open(fileName) as file:
rendataLayer1 = file.read().split("\n")
rendata = []
for i in rendataLayer1:
if not i.startswith("#") and i:
rendata.append(str(i.split()))
t.color("black")
for i in rendata:
i = eval(i)
print(i[0] + " " + i[1])
if i[0] == "cl":
t.color(i[1])
elif i[0] == "fd":
t.fd(int(i[1]))
elif i[0] == "bk":
t.back(int(i[1]))
elif i[0] == "lt":
t.lt(int(i[1]))
elif i[0] == "rt":
t.rt(int(i[1]))
def fd():
distance = s.textinput("Forward", "How far?")
t.fd(int(distance))
with open(fileName, "a") as file:
file.write("fd " + distance + "\n")
def bk():
distance = s.textinput("Backward", "How far?")
t.back(int(distance))
with open(fileName, "a") as file:
file.write("bk " + distance + "\n")
def lt():
distance = s.textinput("Forward", "How much?")
t.lt(int(distance))
with open(fileName, "a") as file:
file.write("lt " + distance + "\n")
def rt():
distance = s.textinput("Forward", "How much?")
t.rt(int(distance))
with open(fileName, "a") as file:
file.write("rt " + distance + "\n")
s.onkeypress(fd, "Up")
s.onkeypress(bk, "Down")
s.onkeypress(lt, "Left")
s.onkeypress(rt, "Right")
s.listen()
s.mainloop()
每当您在 Python 乌龟(或类似的 numinput()
)中调用 textinput()
方法时,它都会撤消您的 listen()
调用,因为 window弹出接受输入成为监听器。只需在每次调用 textinput()
后添加另一个 listen()
调用,事情就会如您所愿。