2017-06-20 113 views
-3

我在python中创建了一个密码系统,并希望输入的密码可以通过键入来输入,但键入时显示为星号。有没有办法做到这一点?有没有一种方法可以在python中输入文字?

+0

在shell中,它不是那么容易的,和细节根据不同的操作系统上。是在GUI中做一个选项吗? –

+1

重新考虑这是否可取。如果你想隐藏密码,那么你为什么要显示它的长度? – cdarke

回答

0

提供您在Windows:

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 

希望这有助于

相关问题