Python Quick Python question - how to change input to *

AI Thread Summary
In Python, achieving masked input (displaying asterisks while typing) can be approached using various methods depending on the environment. The getpass module is typically used for password input but may not work effectively on all terminals, particularly on Windows, leading to warnings about echo control. For GUI applications, Tkinter provides a built-in option for text entry boxes that can mask input with asterisks. If working in a terminal or command line, alternatives like the msvcrt module or sys.stdin can be utilized to manually print asterisks instead of the actual input. The discussion highlights the importance of the chosen environment (terminal vs. GUI) in determining the best method for implementing this feature.
zeion
Messages
455
Reaction score
1

Homework Statement



Hello,

Just a quick question here for Python.
I'm using raw_input to get the user to type in a string, but I want the letters to display as * (like when you enter a password) as they type it. (But not actually register as *). Is this doable in Python?

Thanks.

Homework Equations





The Attempt at a Solution

 
Technology news on Phys.org
You could probably copy and modify the getpass module, which doesn't echo any input to the screen, to echo asterisks instead.

That might only work for terminals, though.
 
Yeah.. can't seen to work on a PC.
Getting this:

Warning (from warnings module):
File "C:\Python27\lib\getpass.py", line 92
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
 
Is there a way to do it with the curses module?
 
Are you using IDLE? You might have to use cmd for that to work.

Otherwise, if you are using stdin, you might be able to use sys.stdin or the msvcrt module to print asterisks instead of input.

What are you trying to do?

You could probably work something out using curses.
 
Actually I'm just trying to write a game where the other player cannot see what was typed. Is there a way to just delete a line that was displayed?
 
Are you doing it in terminal or GUI?
 
Tkinter, IIRC, has an option for a text entry box where you can mask input with asterisks.
 
jhae2.718 said:
Tkinter, IIRC, has an option for a text entry box where you can mask input with asterisks.

I'm doing it in GUI..

Sorry I don't know what those are. Are they Python modules?
 
  • #10
Tkinter is the default GUI toolkit for Python. Sys and msvcrt are Python modules, but if you are doing GUI, the toolkit should have a provision for password entry which should suit your needs.

What toolkit are you using?
 
Back
Top