限制tu中pensize()的最小/最大值

2024-10-17 08:31:55 发布

您现在位置:Python中文网/ 问答频道 /正文

当我按-和+键时,笔的大小保持不变,就像我用海龟画画一样。你知道吗

我用一些似是而非的答案重新解决了这个问题,但没有用。我在网上寻找类似的解决方案,结果空手而归。你知道吗

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()方法的-和+键by函数,在0到20的限制范围内增加/减少它的厚度。感谢您的帮助。你知道吗


Tags: thetotitlehandlersdefourwindowcolor
1条回答
网友
1楼 · 发布于 2024-10-17 08:31:55

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

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

相关问题 更多 >