有没有办法在输入python 3终端时将文本转换为点 我的代码是:
[........
user = input('Enter Your UserName:')
pass = input('Enter Your Password:')
........]
我知道模块getpass。但它不在终端工作,它会发出警告:
Warning (from warnings module):
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
如果它可以在没有警告的情况下工作并隐藏文字,请告诉我 有没有其他类似的东西:
import sys
shell = sys.stdout.shell
shell.show input as '0';
....
我正在创建一个脚本,要求用户提供密码,但如果在输入时显示密码则看起来很糟糕 我在这里希望你能帮助我 如果您想了解更多信息,我愿意为您提供 感谢....
答案 0 :(得分:0)
您无法在Python IDLE内使用getpass
。
同样尝试重定向 stdout 等操作会导致在 IDLE 中重启shell:
import sys
import os
import getpass
sys.stdout = os.devnull
getpass.getpass()
== RESTART: Shell ==
也许您可以使用tkinter
对话框窗口提示用户输入密码:
# import tkinter (a crossplatform GUI)
import tkinter
# import a simple dialog form with a label and a button
# so you don't have to build one yourself
import tkinter.simpledialog
# create an empty main window for GUI,
# without it you will get an error:
# AttributeError: 'NoneType' object has no attribute 'winfo_viewable'
tk_root = tkinter.Tk()
# you don't really need to show it, so hide it immediately
tk_root.withdraw()
# create a dialog window with title 'Password'
# and a text label 'Enter Your Password:'
# also hide typed password with *
passwd = tkinter.simpledialog.askstring('Password','Enter Your Password:', show='*')
只需将其保存为函数:
def get_pass():
import tkinter
import tkinter.simpledialog
tk_root = tkinter.Tk()
tk_root.withdraw()
return tkinter.simpledialog.askstring('Password','Enter Your Password:', show='*')
并使用get_pass()
代替getpass()
。
答案 1 :(得分:0)
分别使用密码输入和tkinter是一个坏主意 我创建了一个只有密码的用户名和密码:
from tkinter import * #(tkinter (A cross-platform GUI)
top = Tk()
def callback(): #what to do after button(Submit) pressed
print(E2.get()) #printing first input
print(E1.get()) #printing second input
top.destroy() #exiting tkinter
top.title('Login')
L1 = Label(top, text="User Name")
L1.grid(row=0, column=0) #setting up position for user name field
E2 = Entry(top, bd = 5)
E2.grid(row=0, column=1)
L1 = Label(top, text="Password") # text for second name,currently Password
L1.grid(row=1, column=0) #setting up position for password field
E1 = Entry(top, bd = 5,show='*') #hidding the text with *
E1.grid(row=1, column=1)
MyButton1 = Button(top, text="Submit", width=10, command=callback) # button named submit
# 'command=callback ' the command you want to do|we have created a function callback
MyButton1.grid(row=3, column=1) # position for button
top.mainloop()
希望这对你有所帮助 Getpass 不适用于 IDLE