有没有一种方法可以替换在python中输入的文本?

时间:2017-06-20 15:28:30

标签: python

我在python中创建一个密码系统,并希望通过输入输入密码,但在输入时显示为星号。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

提供你在窗口:

import msvcrt #for getch
import sys
password = list()
while 1:
    char = msvcrt.getch()#read one char without pressing enter
    password.append(str(char))#append the character to the password
    if ord(char) == 13:#if user presses enter break loop
        break
    if ord(char) == 8:#if user presses backspace
        password.pop()#delete password's last character
        sys.stdout.write("/b")#erase one * from console
        continue#do not write next *
    sys.stdout.write("*")#write * at the place of the character

希望这有帮助