【发布时间】:2019-11-12 15:38:22
【问题描述】:
当我按下 - 和 + 键时,笔的大小保持不变,就像我用海龟画画一样。
我使用一些似是而非的答案重新解决了这个问题,但无济于事。我在互联网上查找了类似的解决方案,但空手而归。
import turtle
turtle.setup(400,500) # Determine the window size
wn = turtle.Screen() # Get a reference to the window
wn.title("Handling keypresses!") # Change the window title
wn.bgcolor("lightgreen") # Set the background color
tess = turtle.Turtle() # Create our favorite turtle
# The next four functions are our "event handlers".
def h1():
tess.forward(30)
def h2():
tess.left(45)
def h3():
tess.right(45)
def h4():
tess.color("red")
def h5():
tess.color("green")
def h6():
tess.color("blue")
def h7():
tess.pensize(0)
def h8():
tess.pensize(20)
def h9():
wn.bye() # Close down the turtle window
def h10():
tess.backward(30)
# These lines "wire up" keypresses to the handlers we've defined.
wn.onkey(h1, "Up")
wn.onkey(h2, "Left")
wn.onkey(h3, "Right")
wn.onkey(h4, "r")
wn.onkey(h5, "g")
wn.onkey(h6, "b")
wn.onkey(h7, "-")
wn.onkey(h8, "+")
wn.onkey(h9, "q")
wn.onkey(h10, "Down")
# Now we need to tell the window to start listening for events,
# If any of the keys that we're monitoring is pressed, its
# handler will be called.
wn.listen()
wn.mainloop()
我正在尝试使用turtle 中的.pensize() 方法,通过.onkey() 方法使用- 和+ 键在0 到20 的受限范围内增加/减少其厚度。任何帮助表示赞赏。
【问题讨论】:
-
h7()和h8()是否按预期将 pensize 更改为 0 和 20?
标签: python max turtle-graphics minimum