首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当onkeypress的if语句不再满足时,我如何停止onkeypress寻找keypress,例如,如何临时停用onkeypress (im new)

当onkeypress的if语句不再满足时,我如何停止onkeypress寻找keypress,例如,如何临时停用onkeypress (im new)
EN

Stack Overflow用户
提问于 2021-03-30 01:52:27
回答 1查看 34关注 0票数 0

我对所有的字母都这样做了,整个序列都在一个函数中。在变量" letter“的第一次迭代期间,它将正常工作,但在第二次迭代、第三次迭代等期间,代码将为变量letter的过去迭代运行fix_fall函数。例如,如果第一次使用letter == "a“,fix_fall将只在您单击"a”时运行,但当您将letter更改为等于"f“时,它将在您单击"a”或"f“时运行fix_fall。我如何让"a“的onkeypress停止激活,直到字母再次等于"a”。

代码语言:javascript
复制
lenny = 0
def draw_an_letter():
  global letter
  global lenny
  apple_xcor = apple.xcor()
  apple_ycor = apple.ycor()
  drawer_xcor = apple_xcor - 14
  drawer_ycor = apple_ycor - 32
  if lenny > 0:
    del letter
    letter = random.choice(letters)
    letter = letter.lower()
  drawer.goto(drawer_xcor, drawer_ycor)
  drawer.color("white")
  drawer.write(letter, font=("Arial", 35, "bold"))
  lenny = lenny + 1 

def fix_fall():
   fall_apple(apple)
   apple_newxcor = random.randint(-130,150)
   apple.goto(apple_newxcor, 40)
   apple.showturtle()
   draw_an_letter()
   wn.listen()
   if letter == "a":
      wn.onkeypress(fix_fall, "a")
   if letter == "b":
      wn.onkeypress(fix_fall, "b")
   if letter == "c":
      wn.onkeypress(fix_fall, "c")
   if letter == "d":
      wn.onkeypress(fix_fall, "d")
   if letter == "e":
      wn.onkeypress(fix_fall, "e")
   if letter == "f":
      wn.onkeypress(fix_fall, "f")
   if letter == "g":
      wn.onkeypress(fix_fall, "g")
   if letter == "h":
      wn.onkeypress(fix_fall, "h")
   if letter == "i":
      wn.onkeypress(fix_fall, "i")
   if letter == "j":
      wn.onkeypress(fix_fall, "j")
   if letter == "k":
      wn.onkeypress(fix_fall, "k")
   if letter == "l":
      wn.onkeypress(fix_fall, "l")
   if letter == "m":
      wn.onkeypress(fix_fall, "m")
   if letter == "n":
      wn.onkeypress(fix_fall, "n")
   if letter == "o":
      wn.onkeypress(fix_fall, "o")
   if letter == "p":
      wn.onkeypress(fix_fall, "p")
   if letter == "q":
      wn.onkeypress(fix_fall, "q")
   if letter == "r":
      wn.onkeypress(fix_fall, "r")
   if letter == "s":
      wn.onkeypress(fix_fall, "s")
   if letter == "t":
      wn.onkeypress(fix_fall, "t")
   if letter == "u":
      wn.onkeypress(fix_fall, "u")
   if letter == "v":
      wn.onkeypress(fix_fall, "v")
   if letter == "w":
      wn.onkeypress(fix_fall, "w")
   if letter == "x":
      wn.onkeypress(fix_fall, "x")
   if letter == "y":
      wn.onkeypress(fix_fall, "y")
   if letter == "z":
      wn.onkeypress(fix_fall, "z")
EN

回答 1

Stack Overflow用户

发布于 2021-03-30 03:14:11

我不知道这是否解决了问题,因为我不知道问题是什么(当你添加更多信息时,我会很高兴地编辑答案),但同时,我在编写这个程序时获得了一些乐趣:它会在屏幕上随机的x位置写下你按下的字母(这看起来有点像你想从你发布的代码片段中做的事情)。

请注意,我使用了一个不同寻常的构造(至少对于python ):一个closure,从而避免了重复26次代码。闭包的确切含义超出了这个答案的范围,但您可以将其(在非常宽泛的术语中)看作是绑定到变量的函数。

function closure_writer 将创建一个新函数,并将其绑定到您调用closure_writer的值letter,然后返回该函数。

对于每个字母,都会调用函数closure_writer,并将返回的函数链接为该字母的回调。

代码语言:javascript
复制
import random
from string import ascii_lowercase
import turtle


def random_xpos():
    """Move the turtle to a random x position."""
    newxcor = random.randint(-600, 600)
    t.setx(newxcor)


def write_letter(letter):
    """Write a letter in the current spot."""
    t.write(letter, font=("Arial", 35, "bold"))


def closure_writer(letter):
    """Return a function that writes the specified letter."""

    def writer():
        """Write the specified letter."""
        random_xpos()
        write_letter(letter)

    # note that we are NOT calling the function, but returning it
    return writer


# setup the turtle
t = turtle.Turtle()
t.penup()
turtle.listen()

# register all the keypress
for letter in ascii_lowercase:
    # closure_writer returns a new function that will be called at each keypress
    # and each function is created with its own letter associated
    turtle.onkeypress(closure_writer(letter), letter)

# start the loop
turtle.mainloop()

干杯!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66859154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档