【问题标题】:How To Show Text While Inputting As Dots In Python Terminal?如何在 Python 终端中输入点时显示文本?
【发布时间】:2017-11-20 02:51:52
【问题描述】:

在 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';
....

我正在创建一个脚本,要求用户提供密码,但如果在键入时显示密码,它看起来很糟糕。
我在这里希望您能帮助我。
如果您想了解更多信息,我愿意提供给您。
谢谢....

【问题讨论】:

标签: python python-3.x input terminal passwords


【解决方案1】:

您不能在Python IDLE 中使用getpass

同时尝试重定向 stdout 会导致 shell 在 IDLE 内重新启动:

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()

【讨论】:

  • @mini 我已更新代码以使用 Python 3.5 和 3.6。
  • 它很完美,但是你能把所有的代码都转换成一个不可读的小行吗?
  • @mini 我已经向那个 sn-p 添加了一些 cmets。 “将所有代码转换为一行不可读的小行”是什么意思?
  • 我们也可以把它做成一个模块。在pypass.py中写函数get_pass()。现在把它放在Lib python36-32 中的 文件夹。现在您可以像这样使用它:import pypasspypass.get_pass()它会起作用。也可以在 1 行中使用。
【解决方案2】:

单独使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2022-08-18
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    相关资源
    最近更新 更多