当我用乌龟画画时,按下-和+键时,笔的大小保持不变。
我使用一些合理的答案重新解决了该问题,但无济于事。我在互联网上寻找类似的解决方案,然后空手而归。
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()
我正尝试在。龟中使用.pensize()方法,通过-on和+键,通过.onkey()方法来在0到20的限制范围内增加/减小其厚度。任何帮助表示赞赏。
答案 0 :(得分:0)
假设h7()
和h8()
有效,而无需更改很多代码,则可以尝试使用全局变量(未经测试的代码)
pensize = 0
def h7():
global pensize
pensize = max(0, pensize-1)
tess.pensize(pensize)
def h8():
global pensize
pensize = min(20, pensize+1)
tess.pensize(pensize)
答案 1 :(得分:0)
您不需要全局变量来跟踪笔的大小。乌龟已经(有效地)是全球实体,并且知道自己的笔大小(经过测试的代码):
def h7():
pensize = tess.pensize() - 1
if pensize >= 0:
tess.pensize(pensize)
def h8():
pensize = tess.pensize() + 1
if pensize <= 20:
tess.pensize(pensize)
但是,还有另一个问题使该解决方案或任何解决方案无法正常工作。这段代码:
wn.onkey(h7, "-")
wn.onkey(h8, "+")
需要改为:
wn.onkey(h7, "minus")
wn.onkey(h8, "plus")
否则,"-"
符号将导致所有未分配的键(包括键入"+"
所需的shift键)来调用h7()
处理程序!此更改还应允许键盘上的等效键起作用。完整的代码:
from turtle import Screen, Turtle
wn = Screen() # Get a reference to the window
wn.setup(400, 500) # Determine the window size
wn.title("Handling keypresses!") # Change the window title
wn.bgcolor("lightgreen") # Set the background color
tess = Turtle() # Create our favorite turtle
# The next nine 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():
pensize = tess.pensize() - 1
if pensize >= 0:
tess.pensize(pensize)
def h8():
pensize = tess.pensize() + 1
if pensize <= 20:
tess.pensize(pensize)
def h9():
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, "minus")
wn.onkey(h8, "plus")
wn.onkey(h9, "Down")
wn.onkey(wn.bye, "q") # Close down the turtle window
# 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()