[PyKDE] KPasswordDialog and KWallet

David Boddie david at boddie.org.uk
Sun Nov 26 16:00:52 GMT 2006

On Sun Nov 26 14:38:32 MET 2006, Hans van Leeuwen wrote: 
> 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? 
Sure. When you instantiate KPasswordDialog, the second argument should 
just be a boolean value - in this case, you want to use True. It's also 
useful to reference the type of password by its name (rather than just 0) 
because it will be easier to read when you come back to the code later: 
def getPass(): 
    p = KPasswordDialog(KPasswordDialog.Password, True, KPasswordDialog.Cancel) 
    p.setPrompt('Enter your password') 
    res = p.exec_loop() 
    if res == KPasswordDialog.Accepted: 
        return p.password() 
        raise MyException, "Password dialog was cancelled" 
Note that I also added a button to the dialog (KPasswordDialog.Cancel). This 
is actually defined in KDialogBase but, since KPasswordDialog inherits it, 
we can access it through that class. Looking at the API, I think it's needed: 
The return value of exec_loop() is either Accepted or Rejected. Again, these 
are defined in a base class - QDialog in this case: 
I changed the way you raise an exception to use an instance of an exception 
class. MyException is just an exception that I've made up for this example; 
I could define it like this: 
class MyException(Exception): 
> 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? 
I don't know why it doesn't work. The getPassword() method is designed to 
be used as a static function (without an instance of KPasswordDialog), so 
you may just be lucky that it mostly works as you expect. 
I think you should be able to do what you need with the function I've 
modified above. Let us know how you get on with it. 

More information about the PyQt mailing list