4

pyqt4 で KWallet を使用する方法を誰かに教えてもらえれば幸いです。

4

2 に答える 2

5

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)
于 2011-08-20T16:52:24.413 に答える
0

ここでそれについての良い例を見つけました。PyQtだけでなくPyKDE4も使用する必要があります。

于 2011-01-31T15:58:38.680 に答える