Python:没有龟窗的onkeypress?

时间:2014-11-07 15:07:30

标签: python window turtle-graphics

我现在的问题是,我想通过命令检测按键

onkeypress(fun,"key")

但是当我导入onkeypress并从乌龟听时,当我运行我的程序时,会弹出一个乌龟窗口。你知道如何再次关闭它,或者怎么不让它出现? 谢谢你希望得到答案,抱歉我的英语不好(我和德国人13岁)

1 个答案:

答案 0 :(得分:0)

在python turtle中找到专家可能很难。但是,如果您不限于该库,则可以使用以下代码来获取用户按下的键(最后一次调用实际上为您获取了密钥):

class _Getch:
    """Gets a single character from standard input.  Does not echo to the
screen."""
    def __init__(self):
        try:
            self.impl = _GetchWindows()
        except ImportError:
            self.impl = _GetchUnix()

    def __call__(self): return self.impl()


class _GetchUnix:
    def __init__(self):
       import tty, sys

    def __call__(self):
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch


class _GetchWindows:
    def __init__(self):
        import msvcrt

    def __call__(self):
        import msvcrt
        return msvcrt.getch()


getch = _Getch()

代码来自this article