I am working with PyQT6. I am trying to get the text from checkbox that change a label, for example "You have selected: " value
. So the question is, how to convert these if conditionals to a switch case?
Here is the method of the if conditionals
def item_selected(self):
value = ""
if self.check1.isChecked():
value = self.check1.text()
if self.check2.isChecked():
value = self.check2.text()
if self.check3.isChecked():
value = self.check3.text()
if self.check4.isChecked():
value = self.check4.text()
self.label.setText("You have selected: " value)
How to implement this code with switch case? I tried this:
def item_selected(self):
value = ""
match value:
case bool(self.check1.isChecked()):
value = (self.check1.text())
case bool(self.check2.isChecked()):
value = (self.check2.text())
case bool(self.check3.isChecked()):
value = (self.check3.text())
case bool(self.check4.isChecked()):
value = (self.check4.text())
self.label.setText("You have selected: " value)
but I'm not getting the values when I check the boxes
CodePudding user response:
I don't see how this expression can be converted to switch-conditional since it is based on the state of a variable. Also if you want to reduce the expression then you can do it with a for-loop:
value = ""
for checkbox in (self.check4, self.check3, self.check2, self.check1):
if checkbox.isChecked():
value = checkbox.text()
break
self.label.setText(f"You have selected: {value}")