我是 Python 和 PyQt 的新手,这是我的第一个应用程序。当前,当单击按钮时,会toggleLED()
检查self.LedOn
以决定是否打开 LED。
class Screen(QWidget):
def initUI(self):
self.LedOn = 0
btn = QPushButton('Off', self)
btn.setCheckable(True)
btn.clicked.connect(self.toggleLED)
def toggleLED(self, pressed):
source = self.sender()
if self.LedOn:
source.setText('Off')
source.LedOn = 0
self.serial.write('L')
else:
source.setText('On')
source.LedOn = 1
self.serial.write('H')
问题:现在如果我们想控制多个 LED 并记住它们各自的切换状态,最佳实践是什么?
我似乎也无法将附加值传递给toggleLED()
withbtn.clicked.connect(self.toggleLED(btnNumber))
class Screen(QWidget):
def initUI(self):
btn1 = QPushButton('Off', self)
btn1.setCheckable(True)
btn1.LedOn= 0
btn1.clicked.connect(selftoggleLED(1))
btn2 = QPushButton('Off', self)
btn2.setCheckable(True)
btn2.LedOn= 0
btn2.clicked.connect(selftoggleLED(2))
def toggleLED(self, pressed, value):
source = self.sender()
if self.LedOn:
source.setText('Off')
self.serial.write(value)
self.serial.write('L')
else:
source.setText('On')
self.serial.write(value)
self.serial.write('H')