0

Is there Any way to convert Text to Dots while Inputting In python 3 Terminal.
my Code is:

[........
user = input('Enter Your UserName:')
pass = input('Enter Your Password:')
........]  

I know the Module getpass.But it don't work in Terminal , It gives warning:

Warning (from warnings module):
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.  

If it can work without warning and hide text,Please tell me.
Is there any Otherway somthing like:

import sys
shell = sys.stdout.shell
shell.show input as '0';
....

I am creating a script that asks the user to give Password but it looks bad if Password is being showed while typing.
I am here with a hope that you may help me.
If you want more information,I am ready to provide to you.
Thanks....

  • What terminal and OS? – mx0 Jun 17 '17 at 12:05
  • Possible duplicate of ["GetPassWarning: Can not control echo on the terminal" when running from IDLE](https://stackoverflow.com/questions/38878741/getpasswarning-can-not-control-echo-on-the-terminal-when-running-from-idle) – mx0 Jun 17 '17 at 12:06
  • I am on windows.Check this [Image](https://postimg.org/image/f86ngmmez/f97b4fac/). –  Jun 17 '17 at 12:09
  • Any other way Please? –  Jun 17 '17 at 12:11

2 Answers2

1

You can't use getpass inside Python IDLE.

Also trying things like redirecting stdout causes shell restart inside IDLE:

import sys
import os
import getpass

sys.stdout = os.devnull
getpass.getpass()

== RESTART: Shell ==

Maybe you can use tkinter dialog window to prompt user for password:

# 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='*')

Just save it as a function:

def get_pass():
    import tkinter
    import tkinter.simpledialog
    tk_root = tkinter.Tk()
    tk_root.withdraw()
    return tkinter.simpledialog.askstring('Password','Enter Your Password:', show='*')

and use get_pass() instead of getpass().

mx0
  • 6,445
  • 12
  • 49
  • 54
  • @mini I've updated code to work with Python 3.5 & 3.6. – mx0 Jun 17 '17 at 14:15
  • Its perfect but can you convert all the code to one unreadable small line? –  Jun 17 '17 at 14:51
  • @mini I've added some comments to that snippet. What do you mean by "convert all the code to one unreadable small line"? – mx0 Jun 19 '17 at 17:48
  • We can Also Make It Into A module.Write The Function **get_pass()** in **pypass.py** .Now Place It In _Lib_ folder in _python36-32_ .Now You can use It Like: `import pypass` `pypass.get_pass()`and it will work.Also In 1 line. –  Jun 26 '17 at 04:12
0

Using password input with tkinter separately is a bad idea.
I created this one in which it has username and password except password only:

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

Hope It will be helpful for you.
Getpass is not for IDLE