在 Python 中使用图形对话框询问用户密码的最简单方法? [英] Simplest method of asking user for password using graphical dialog in Python?

查看:50
本文介绍了在 Python 中使用图形对话框询问用户密码的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个将在后台静默运行的备份守护进程.该守护程序依赖于 duplicity 备份软件,备份软件时需要加密密钥.我无法通过控制台询问密码,因为显然守护进程无法访问密码.

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.

如何轻松创建一个提示,要求用户输入密码并将其返回给应用程序(通过 Python 变量)?我使用的是 Python 2.7.

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.

推荐答案

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

这篇关于在 Python 中使用图形对话框询问用户密码的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆