2

私は Python 3.3 と PyQt 4.10.1 を使用しています。下の図はPyQt bookからのものです。

以下に示すように、5 つのボタンがあるとします。それぞれをクリックすると、ラベルのテキストがボタン番号を含むコンテキストに変更されます。たとえば、ユーザーがキャプション「4」が付いたボタンをクリックすると、ラベルが次のように変更されます。You clicked button 'Four'

ここに画像の説明を入力

ボタンごとにシグナルスロットを作成する代わりに、パラメーターを受け入れる一般化されたメソッドが作成され、partial()メソッドが使用されます。

    ...
    self.label = QLabel("Click on a button.")
    self.button1 = QPushButton("One")
    ...
    self.button5 = QPushButton("Five")
    self.connect(self.button1, SIGNAL("clicked()")
                                , partial(self.anyButton, "One"))
    ...
    self.connect(self.button5, SIGNAL("clicked()")
                                , partial(self.anyButton, "Five"))
    ...

def anyButton(self, buttonNumber):
    self.label.setText("You clicked button '%s'" % buttonNumber)

partial(self.anyButton, "One")に変更したいときはいつでもself.anyButton("One")、次のようなエラーが発生します。

Traceback (most recent call last):
  File "C:\Users\abdullah\Desktop\test.py", line 47, in <module>
    form = Form()
  File "C:\Users\abdullah\Desktop\test.py", line 20, in __init__
    , self.anyButton("One"))
TypeError: arguments did not match any overloaded call:
  QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoC
onnection): argument 3 has unexpected type 'NoneType'
  QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnecti
on): argument 3 has unexpected type 'NoneType'
  QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection
): argument 3 has unexpected type 'NoneType'

これの理由は何ですか?関数を直接呼び出せないのはなぜですか? また、なぜpartial()メソッドが機能するのですか?

4

1 に答える 1

4

partial引数が置換された関数 を返します。anyButton

self.anyButton("One")関数によって返される値を提供します。

于 2013-06-08T10:55:33.063 に答える