在PyQt4中使用KWallet

2024-05-03 12:04:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: pyqt4kwallet
2条回答

Python命令行教程

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

$ 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模块可以打开钱包,获取并设置密码:

^{pr2}$

可按以下方式使用:

$ python
>>> import kwallet_example
>>> wallet = kwallet_example.open_wallet()
>>> kwallet_example.set_password(wallet, 'mypass')
>>> kwallet_example.get_password(wallet)

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

相关问题 更多 >