【问题标题】:Limit the min/max of pensize() in turtle限制乌龟中 pensize() 的最小值/最大值
【发布时间】: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


【解决方案1】:

您不需要全局变量来跟踪画笔大小。海龟已经(有效地)是全球实体并且知道自己的笔大小(经过测试的代码):

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()

【讨论】:

  • 感谢您的反馈。我不知道我需要在键绑定中使用单词而不是字符!这真的让我感到惊讶,因为我从未怀疑这是错误的一部分。
猜你喜欢
  • 2019-06-23
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
  • 2013-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多