-1
:(チェック)は1つの必要な位置引数が不足して例外TypeError:私は小さなPyQt4アプリを作ってるんだし、これまでのところ、私はこれを持っている「theText」
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.initUi()
def initUi(self):
self.setFixedSize(500, 300)
self.text = QtGui.QLabel('Text to be changed')
self.text2 = QtGui.QLabel('Also to be changed')
self.textCheckBox = QtGui.QCheckBox()
self.textCheckBox.stateChanged.connect(self.check(self.text))
self.show()
def check(self, state, theText):
if state == QtCore.Qt.Checked:
theText.setStyleSheet("color: pink")
else:
theText.setStyleSheet("color: black")
def main():
q = QtGui.QApplication(sys.argv)
w = Window()
sys.exit(q.exec())
if __name__=='__main__':
main()
問題は私の.connectラインにはない、あります私はそれに渡すパラメータを認識したいと思うようだと私はなぜわからない。多くのQLabel引数を渡したいので、関数内でテキストを直接参照することはできません。したがって、theTextパラメータを作成しました。私はどんな助けにも感謝しています。
パラメータなしで動作バージョン:
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.initUi()
def initUi(self):
self.setFixedSize(500, 300)
self.text = QtGui.QLabel('Text to be changed')
self.text2 = QtGui.QLabel('Also to be changed')
self.textCheckBox = QtGui.QCheckBox()
self.textCheckBox.stateChanged.connect(self.check)
self.hbox = QtGui.QHBoxLayout()
self.hbox.addWidget(self.text)
self.hbox.addWidget(self.textCheckBox)
self.setLayout(self.hbox)
self.show()
def check(self, state):
if state == QtCore.Qt.Checked:
self.text.setStyleSheet("color: pink")
else:
self.text.setStyleSheet("color: black")
def main():
q = QtGui.QApplication(sys.argv)
w = Window()
sys.exit(q.exec())
if __name__=='__main__':
main()
'self.textCheckBox.stateChanged.connect(self.check(self.text))'行の 'state'引数を忘れたようです。 –
私はそれを渡す必要があるとは思っていません。私の初期のバージョンの関数は渡されることなく機能していました。 – WewLad
しかし、あなたは 'check'メソッドを定義してそれを渡す必要があります。 –