Search code examples
pythonpython-2.7passwords

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.


Solution

  • 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