-3
我在python中創建了一個密碼系統,並希望輸入的密碼可以通過鍵入來輸入,但鍵入時顯示爲星號。有沒有辦法做到這一點?有沒有一種方法可以在python中輸入文字?
我在python中創建了一個密碼系統,並希望輸入的密碼可以通過鍵入來輸入,但鍵入時顯示爲星號。有沒有辦法做到這一點?有沒有一種方法可以在python中輸入文字?
提供您在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
希望這有助於
在shell中,它不是那麼容易的,和細節根據不同的操作系統上。是在GUI中做一個選項嗎? –
重新考慮這是否可取。如果你想隱藏密碼,那麼你爲什麼要顯示它的長度? – cdarke