[PyKDE] KPasswordDialog and KWallet
Hans van Leeuwen
hanz at hanz.nl
Sun Nov 26 13:38:32 GMT 2006
On Saturday 25 November 2006 20:12, David Boddie wrote:
> On Fri, 24 Nov 2006 17:43:38 +0100, Hans van Leeuwen wrote:
> >Let me start by saying I just started using PyKDE and it's been a lot of
> > fun. I have never used Python, nor done any GUI-progamming until last
> > week, and I've already managed to create a few useable programs.
>
> You're a quick learner! It's good to hear that you're enjoying yourself.
> :-)
>
> >The question I have is regarding KPasswordDialog and KWallet. I'm writing
> > a program which asks the user for a password, and passes it on to an
> > external application, which returns an error if the password is
> > incorrect.
> >
> >I would really like to be able to store the password using KWallet, but I
> >can't figure out how to do that.
>
> Looking at the API, I can see your problem.
>
> >I use the following function:
> >
> >def getPass():
> > password = QCString();
> > prompt = "Enter your password"
> > KPasswordDialog.getPassword(password, prompt)
> > return password
> >
> >It asks for the password and returns the entered value, but there is no
> > "Keep password" checkbox.
> >
> >If I use "password = 'foobar'", the checkbox appears, but this is not what
> > I want, because I don't know the correct password beforehand.
>
> Yes, I also found I could pass a null QCString and get a checkbox as well,
> but that's useless because you then have no way of accessing the string
> afterwards. (I believe the implementation modifies the string itself.)
>
> >In the KpasswordDialog API-docs I see an option "enableKeep", but I have
> > no idea how to use it.
>
> QDialog-based classes use a convention where a pointer or reference to
> a value is passed to some static function in C++ so that the function can
> fill it in, and you can then look at the result afterwards. In Python,
> these functions typically return a tuple instead, containing a value
> indicating success or failure. In this case, you also get the value
> corresponding to the "Keep password" checkbox, though you don't seem to be
> able to specify that.
>
> Maybe that's a problem with the PyKDE API, but there is a simple
> workaround:
>
> 1. Create a dialog using one of the standard constructors. The first
> one looks OK. Pass True for the enableKeep argument.
> 2. Set any text using setPrompt().
> 3. Call the dialog's exec_loop() method, obtaining a value that
> indicates whether the user accepted or cancelled the dialog.
> 4. Call the dialog's password() method to get the password.
>
> It's not quite as straightforward as using the static function, but you
> get more control over the process as a result.
Thanks for your answer!
You mean something like this?
def getPass():
p = KPasswordDialog (0, 'enableKeep')
p.setPrompt('Enter your password')
res = p.exec_loop()
if not res:
raise "Password dialog was cancelled"
else:
return p.password()
The "keep password"-checkbox doesn't appear. Probably because I
use 'enableKeep' in the wrong way. Could you please give me an example of how
to do this correctly?
After playing around a bit I discovered that the "keep password"-checkbox does
appear when I use the following function:
def getPass():
password = QCString();
prompt = "Enter your password"
p = KPasswordDialog(0, prompt)
p.getPassword(password, prompt)
return password, p.keep()
The problem is p.keep() is always false, even when I check the checkbox.
What am I doing wrong here?
Hans
More information about the PyQt
mailing list