在 Python 乌龟事件处理程序中比较坐标时出现 TypeError
Getting TypeError comparing coordinates in Python turtle event handler
我不知道如何将 x 和 y 转换为来自字符串的整数
import turtle
t = turtle.Turtle()
s = turtle.Screen()
t.penup()
t.speed(0)
def draw_grid():
count = 0
distance = 0
while count < 5:
t.goto(0, distance)
for i in range(5):
t.dot()
t.fd(100)
count += 1
distance += 100
draw_grid()
def get_mouse_click_coor(x, y):
这里发生了什么事??我不确定,一直给我这个错误:
TypeError: 'int' 和 'str'
实例之间不支持“<”
X = int(x)
Y = int(y)
if -20 < X < 20 and 380 > Y > 20:
t.penup()
if 80 > Y > 20:
t.goto(0,0)
t.pendown()
t.goto(0,100)
if 180 > Y > 120:
t.goto(0, 100)
t.pendown()
t.goto(0, 200)
if 280 > Y > 220:
t.goto(0, 200)
t.pendown()
t.goto(0, 300)
if 380 > Y > 320:
t.goto(0, 300)
t.pendown()
t.goto(0, 400)
turtle.onscreenclick(get_mouse_click_coor)
s.mainloop()
s.done()
(关闭 window 时出现 "AttributeError: '_Screen' object has no attribute 'done'" 错误除外)您的代码在我的系统 (Apple OS X) 上按预期工作,无需修改。以下是我对您的代码进行的样式和效率修改,看看它是否更适合您:
from turtle import Turtle, Screen
def draw_grid():
for distance in range(0, 500, 100):
turtle.goto(0, distance)
for _ in range(5):
turtle.dot()
turtle.fd(100)
def get_mouse_click_coor(x, y):
if -20 < x < 20 and 20 < y < 380:
turtle.penup()
if 20 < y < 80:
turtle.goto(0, 0)
turtle.pendown()
turtle.goto(0, 100)
elif 120 < y < 180:
turtle.goto(0, 100)
turtle.pendown()
turtle.goto(0, 200)
elif 220 < y < 280:
turtle.goto(0, 200)
turtle.pendown()
turtle.goto(0, 300)
elif 320 < y < 380:
turtle.goto(0, 300)
turtle.pendown()
turtle.goto(0, 400)
screen = Screen()
turtle = Turtle()
turtle.penup()
turtle.speed('fastest')
draw_grid()
screen.onscreenclick(get_mouse_click_coor)
screen.mainloop()
您不需要同时使用 mainloop()
和 done()
,因为它们是相同的东西(别名。)
我不知道如何将 x 和 y 转换为来自字符串的整数
import turtle
t = turtle.Turtle()
s = turtle.Screen()
t.penup()
t.speed(0)
def draw_grid():
count = 0
distance = 0
while count < 5:
t.goto(0, distance)
for i in range(5):
t.dot()
t.fd(100)
count += 1
distance += 100
draw_grid()
def get_mouse_click_coor(x, y):
这里发生了什么事??我不确定,一直给我这个错误:
TypeError: 'int' 和 'str'
实例之间不支持“<” X = int(x)
Y = int(y)
if -20 < X < 20 and 380 > Y > 20:
t.penup()
if 80 > Y > 20:
t.goto(0,0)
t.pendown()
t.goto(0,100)
if 180 > Y > 120:
t.goto(0, 100)
t.pendown()
t.goto(0, 200)
if 280 > Y > 220:
t.goto(0, 200)
t.pendown()
t.goto(0, 300)
if 380 > Y > 320:
t.goto(0, 300)
t.pendown()
t.goto(0, 400)
turtle.onscreenclick(get_mouse_click_coor)
s.mainloop()
s.done()
(关闭 window 时出现 "AttributeError: '_Screen' object has no attribute 'done'" 错误除外)您的代码在我的系统 (Apple OS X) 上按预期工作,无需修改。以下是我对您的代码进行的样式和效率修改,看看它是否更适合您:
from turtle import Turtle, Screen
def draw_grid():
for distance in range(0, 500, 100):
turtle.goto(0, distance)
for _ in range(5):
turtle.dot()
turtle.fd(100)
def get_mouse_click_coor(x, y):
if -20 < x < 20 and 20 < y < 380:
turtle.penup()
if 20 < y < 80:
turtle.goto(0, 0)
turtle.pendown()
turtle.goto(0, 100)
elif 120 < y < 180:
turtle.goto(0, 100)
turtle.pendown()
turtle.goto(0, 200)
elif 220 < y < 280:
turtle.goto(0, 200)
turtle.pendown()
turtle.goto(0, 300)
elif 320 < y < 380:
turtle.goto(0, 300)
turtle.pendown()
turtle.goto(0, 400)
screen = Screen()
turtle = Turtle()
turtle.penup()
turtle.speed('fastest')
draw_grid()
screen.onscreenclick(get_mouse_click_coor)
screen.mainloop()
您不需要同时使用 mainloop()
和 done()
,因为它们是相同的东西(别名。)