在使用Python的过程中,想输入账号和密码,但是密码会随着输入显示在屏幕上,为了解决这个问题需要用到msvcrt模块

这里是使用代码

import  msvcrt, sys

def pwd_input(a):
    print str(a),
    chars = []
    while True:
        newChar = msvcrt.getch()
        if newChar in '\r\n':
            print ''
            break
        elif newChar in '\b':
            if chars:
                del chars[-1]
                sys.stdout.write('\b\b')
        else:
            chars.append(newChar)
            sys.stdout.write('*')
    return str(chars)

pwd = pwd_input('password:')

这样就解决了显示问题。

相关文章:

  • 2022-02-17
  • 2022-01-02
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-15
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-21
  • 2022-12-23
  • 2021-12-23
相关资源
相似解决方案