gpt4 book ai didi

python - 在 PyQt4 中使用 KWallet

转载 作者:太空狗 更新时间:2023-10-30 02:50:33 29 4
gpt4 key购买 nike

如果有人能告诉我如何将 KWallet 与 pyqt4 一起使用,那就太好了

最佳答案

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

#!/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)

可以通过以下方式使用:

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

关于python - 在 PyQt4 中使用 KWallet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2147724/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com