如何使用一键完成两件事就按Python 3

时间:2018-02-15 03:37:06

标签: python python-3.x

如何使用一个screen.onkeypress运行两个命令? 这是我要问的一个例子;

def commandtwo():
    run_second_command()

def commandone():
    run_first_command()

screen.listen()
screen.onkeypress(#Here I want to run both commands by pressing one key)

3 个答案:

答案 0 :(得分:3)

你能不能这样做:

def do_commands():
    run_first_command()
    run_second_command()
screen.listen()
screen.onkeypress(do_commands)

答案 1 :(得分:1)

为什么不创建一个命令来调用另外两个?

def button_action():
    commandone()
    commandtwo()


def commandtwo():
    run_second_command()

def commandone():
    run_first_command()

screen.listen()
screen.onkeypress(button_action, key=KEY)

或者更糟糕的想法是将它们链接起来?

def commandtwo():
    run_second_command()

def commandone():
    run_first_command()
    commandtwo()

screen.listen()
screen.onkeypress(commandone, key=KEY)

答案 2 :(得分:-1)

您需要执行此代码。确保screen.listen遵循onkeypress方法,因为屏幕需要有焦点才能捕获那些按下,例如[turtle docs](https://docs.python.org/3.1/library/turtle.html#turtle.onkeypress)说

def do_commands():
    run_first_command()
    run_second_command()
screen.onkeypress(do_commands())
screen.listen()