美文网首页Python代码
Python实现控制台密码星号输入

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

作者: JetLu | 来源:发表于2015-12-18 14:48 被阅读1711次
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

相关文章

网友评论

    本文标题:Python实现控制台密码星号输入

    本文链接:https://www.haomeiwen.com/subject/ptvphttx.html