首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PyQt4中使用KWallet

在PyQt4中使用KWallet
EN

Stack Overflow用户
提问于 2010-01-27 22:44:59
回答 2查看 1.1K关注 0票数 4

如果有人能告诉我如何在pyqt4中使用KWallet,那就太好了

EN

回答 2

Stack Overflow用户

发布于 2011-08-21 00:52:24

Python命令行中的教程

首先,我将展示如何从Python命令行使用kwallet来读取和写入密码:

代码语言:javascript
复制
$ python

# We import the necessary modules.
>>> from PyKDE4.kdeui import KWallet
>>> from PyQt4 import QtGui

# We create a QApplication. We will not use it, but otherwise
# we would get a "QEventLoop: Cannot be used without
# QApplication" error message.
>>> app = QtGui.QApplication([])

# We open the wallet.
>>> wallet = KWallet.Wallet.openWallet(
                 KWallet.Wallet.LocalWallet(), 0)

# We create a folder in which we will store our password,
# and set it as current.
>>> wallet.createFolder('myfolder')
True
>>> wallet.hasFolder('myfolder')
True
>>> wallet.setFolder('myfolder')
True

# We read the password (which does not exist yet), write it,
# and read it again.
>>> wallet.readPassword('mykey')
(0, PyQt4.QtCore.QString(u''))
>>> wallet.writePassword('mykey', 'mypassword')
0
>>> wallet.readPassword('mykey')
(0, PyQt4.QtCore.QString(u'mypassword'))

作为Python模块的教程

通常,您希望创建一些简单的函数来包装kwallet方法。下面的Python模块可以打开钱包,获取并设置密码:

代码语言:javascript
复制
#!/usr/bin/python

from PyKDE4.kdeui import KWallet
from PyQt4 import QtGui

def open_wallet():
    app = QtGui.QApplication([])
    wallet = KWallet.Wallet.openWallet(
                 KWallet.Wallet.LocalWallet(), 0)
    if not wallet.hasFolder('kwallet_example'):
        wallet.createFolder('kwallet_example')
    wallet.setFolder('kwallet_example')
    return wallet

def get_password(wallet):
    key, qstr_password = wallet.readPassword('mykey')

    # converting the password from PyQt4.QtCore.QString to str
    return str(qstr_password)

def set_password(wallet, password):
    wallet.writePassword('mykey', password)

它的使用方法如下:

代码语言:javascript
复制
$ python
>>> import kwallet_example
>>> wallet = kwallet_example.open_wallet()
>>> kwallet_example.set_password(wallet, 'mypass')
>>> kwallet_example.get_password(wallet)
票数 5
EN

Stack Overflow用户

发布于 2011-01-31 23:58:39

我在here中找到了一个很好的例子,你不仅需要使用PyQt,还需要使用PyKDE4。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2147724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档