Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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
.
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='*')
–
–
pwdbox.pack(side = 'top')
pwdbox.bind('<Return>', onpwdentry)
Button(root, command=onokclick, text = 'OK').pack(side = 'top')
root.mainloop()
return password
–
–
–
–
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
pwdbox.pack(side = 'top')
pwdbox.bind('<Return>', onpwdentry)
Button(root, command=onokclick, text = 'OK').pack(side = 'top')
root.mainloop()
return PASSWORD
Expanding on Diego's answer with some minimal housekeeping (without this I was getting crashes galore trying to use his beautifully brief example):
import Tkinter, tkSimpleDialog
root = Tkinter.Tk() # dialog needs a root window, or will create an "ugly" one for you
root.withdraw() # hide the root window
password = tkSimpleDialog.askstring("Password", "Enter password:", show='*', parent=root)
root.destroy() # clean up after yourself!
This will work well from a program that is otherwise just a terminal / console application.
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.