How To Prompt For Passwords In Python (GUI)

Hey guys, I’ll be showing you a quick and easy way to get a password prompt GUI screen in Python using an additional module named PyAutoGUI. This is a cross-platform GUI automation for human beings, makes life much easier.


First of course you need to install the module. That you can do using pip install pyautogui.

Quick Note: If you get an import error for PIL during the installation of the script install the Python Image module first, using pip install Image.

The Program

Hopefully you have installed PyAutoGUI successfully. Now let’s get started with the more fun part, first a basic example.

import pyautogui

pyautogui.password(text="Enter the magic word", title="Password Required", default="", mask="*")

What’s going on in the above code? Let’s dissect it a little.

In the first line we import the pyautogui module which contains the main functionality.

In the second line  we call the password method from pyautogui module. The password method takes the parameters “text” which is the text to be displayed when you prompt for the password, “title” which is the title of the window being displayed, “default” which is the default value in the text box and “mask” which is the character that will mask the entered text. The window will look like this:


Now let’s get a little more complex so that you can implement this in your script.

from pyautogui import password
from sys import exit

originalPassword = "supersecretpassword"
attempts = 0

while True:
  inputPassword = password(text="Enter the magic word (Attempts left: {0})".format(3 - attempts), \
    title="Password Required", default="", mask="*")

  attempts += 1

  if inputPassword == originalPassword:
    print "Access granted!"
    print "Secret message: Python is awesome"
  elif attempts == 3:
    print "Wrong password, you've exceeded the max number of tries!"
  elif inputPassword == None:
    print "Wrong password!"

So what did we do now? Here we’re specifically importing the “password” function from pyautogui and also import “exit” function from sys module, since we’ll need it. Now next we declare “originalPassword” to some string which is our password.

After that we start an infinite while loop and set “inputPassword” to the input received from the password box. Then we add 1 to the “attempts” variable each time we execute the loop, if attempts reaches 3 then the user can’t enter anymore passwords and the script exits. Similarly if we enter the correct password then the “Access granted!” line is printed and we get the secret message.

We also need to add a response for when the user presses the “Cancel” button, in which None is returned. So we use an else if conditional statement to check if the input is None and exit if true.


This may not be the best password protected script ever but it’s pretty much enough to give you the idea about how this stuff works. You can use it for many purposes or just for fun, like scripting a word guessing game or stuff.

Source: Areeb Beigh