【问题标题】:Python: onkeypress without turtle window?Python:没有乌龟窗口的onkeypress?
【发布时间】:2014-11-07 15:07:29
【问题描述】:

我此时的问题是,我想通过命令检测按键

onkeypress(fun,"key")

但是当我导入 onkeypress 并从 turtle 监听时,当我运行我的程序时,会弹出一个海龟窗口。你知道如何再次关闭它,或者如何不让它出现吗? 感谢您的回答,抱歉我的英语不好(我是德国人,13 岁)

【问题讨论】:

    标签: python window turtle-graphics


    【解决方案1】:

    在 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

    【讨论】:

      猜你喜欢
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      相关资源
      最近更新 更多