Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest method of asking user for password using graphical dialog in Python?

I'm developing a backup daemon that will run silently in the background. The daemon relies on the duplicity backup software, which when backing up requires an encryption key. I cannot ask for the password through the console because obviously, the daemon has no access to such.

How could I easily create a prompt that asks the user to type in a password, and returns it to the application (through a Python variable)? I'm using Python 2.7.

like image 626
liamzebedee Avatar asked Mar 30 '13 23:03

liamzebedee


People also ask

How do I ask for a password in Python?

Using getpass() function to prompt user password The getpass() function is used to prompt to users using the string prompt and reads the input from the user as Password. The input read defaults to “Password: ” is returned to the caller as a string.

How do I ask for a username and password in Python?

To prompt the user for username and password: Use the input() function to take non-sensitive input values, such as the username. Use the getpass() method to take sensitive values, such as the password.

How do you ask for input in python GUI?

Explanation. First, we are importing the Tkinter module, then we are creating a window in the ROOT object. Next, we have the withdraw() method which removes the window from the screen (without destroying it). Later we are taking the user from the user using askstring() method which simply takes the string entered.


3 Answers

Because you asked for the simplest (Python 2.7):

import Tkinter, tkSimpleDialog
tkSimpleDialog.askstring("Password", "Enter password:", show='*')

For Python 3.3:

import tkinter
tkinter.simpledialog.askstring("Password", "Enter password:", show='*')

For Python 3.6+:

import tkinter as tk
import tkinter.simpledialog
tk.Tk().withdraw()
tkinter.simpledialog.askstring("Password", "Enter password:", show='*')
like image 64
Diego Torres Milano Avatar answered Nov 15 '22 20:11

Diego Torres Milano


from Tkinter import *

def getpwd():
    password = ''
    root = Tk()
    pwdbox = Entry(root, show = '*')
    def onpwdentry(evt):
         password = pwdbox.get()
         root.destroy()
    def onokclick():
         password = pwdbox.get()
         root.destroy()
    Label(root, text = 'Password').pack(side = 'top')

    pwdbox.pack(side = 'top')
    pwdbox.bind('<Return>', onpwdentry)
    Button(root, command=onokclick, text = 'OK').pack(side = 'top')

    root.mainloop()
    return password
like image 33
pycoder112358 Avatar answered Nov 15 '22 19:11

pycoder112358


Because not everyone wants to use TK, here's a script using PyQt:

from PyQt5.QtWidgets import QApplication, QInputDialog, QLineEdit
import sys
app = QApplication(sys.argv)
qd = QInputDialog()
qd.setTextEchoMode(QLineEdit.Password)
qd.show()
app.exec()

And, because you wouldn't usually just ask a user for a password just for the heck of it:

#!/bin/env python3
#passwordPrompt.py

from PyQt5.QtWidgets import QApplication, QInputDialog
import sys, time

def succFunc():
  sys.stdout.write(qd.textValue())
  sys.stdout.flush()
  exit(0)

def failFunc():
  exit(1)

app = QApplication(sys.argv)
qd = QInputDialog()
#QLineEdit.Password
qd.setTextEchoMode(2)
qd.rejected.connect(failFunc)
qd.accepted.connect(succFunc)
qd.show()
app.exec()

And the corresponding bash function:

#!/bin/bash

passwordPrompt.py | tee
like image 22
smaudet Avatar answered Nov 15 '22 20:11

smaudet