首页 > 解决方案 > Windows 中 python 的 termios 模块是什么?

问题描述

首先,代码(我在这里找到:https ://github.com/defaultxr/taptempo.py/blob/master/taptempo.py )

#!/usr/bin/env python

from __future__ import print_function

import tty
import termios
from time import time
from sys import stdin


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


def addtime(times):
    if type(times) != list:
        raise(TypeError)
    t = time()
    if len(times) == 0:
        tdiff = 0  # initial seed
    else:
        tdiff = t - times[-1][0]
    return (t, tdiff)


def averagetimes(times):
    averagetime = sum([row[1] for row in times])/float(len(times))
    bpm = (1.0/(averagetime/60.0))
    return (averagetime, bpm)


def main():
    print('Tap a key on each beat. Press q to quit.', end='\r')

    times = []
    while True:
        char = getchar()
        if char in ('q', 'Q', '\x1b', '\x03'):  # q, Q, ESC, Control+C
            print()
            quit()

        times.append(addtime(times))
        if len(times) > 1:
            # remove first element if it's either the initial seed
            # or when the list reaches max length
            if times[0][1] == 0 or len(times) > 16:
                del times[0]
            (averagetime, bpm) = averagetimes(times)

            print("\rDetected BPM: %0.3f (Avg time between each: %0.3fs)"
                  % (bpm, averagetime), end='')


if __name__ == '__main__':
    main()

我收到一条错误消息:“错误:找不到满足 termios 要求的版本错误:找不到 termios 的匹配发行版”

经过一番研究,事实证明,termios 是 Python 附带的,但它不包含在 Windows 发行版中(它只在 Linux 中),因为另一个名为“Airflow”的实用程序也不在 Windows 中。

我是一名音乐家,它看起来像是一个非常轻量级的小应用程序,非常适合“节奏敲击”。包含节奏敲击器的较大程序通常过于庞大且不必要,有时会有点慢。所以我只是想知道是否有办法让那个小应用程序在 Windows 中运行?

无论如何,这是一个有趣的问题。我从来没有在 Python 中遇到过 Windows 和 Linux 之间这样的“冲突”(如果可以这样称呼的话)。

标签: python

解决方案


推荐阅读