I am new to Python and PyQt, and this is my first application. Currently, pressing the toggleLED()
button checks self.LedOn
to decide whether to turn on the 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')
Problem: Now, if we want to control several LEDs and remember their individual switching state, what is the best practice?
I also cannot pass the extra toggleLED()
value using btn.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')
source share