热门搜索 :
考研考公
您的当前位置:首页正文

Python实现控制台密码星号输入

来源:东饰资讯网
import msvcrt, sys, os
print('password: ', end='', flush=True)

li = []

while 1:
    ch = msvcrt.getch()
    #回车
    if ch == b'\r':
        msvcrt.putch(b'\n')
        print('输入的密码是:%s' % b''.join(li).decode())
        break
    #退格
    elif ch == b'\x08':
        if li:
            li.pop()
            msvcrt.putch(b'\b')
            msvcrt.putch(b' ')
            msvcrt.putch(b'\b')
    #Esc
    elif ch == b'\x1b':
        break
    else:
        li.append(ch)
        msvcrt.putch(b'*')

os.system('pause')

示例

screenshot
Top