私は 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()
メソッドが機能するのですか?