我想创建一个带有4个单选按钮的if-else语句脚本:
radioButton_1, radioButton_2, radioButton_3, radioButton_4下面是我的代码:
def rb_check(self):
rb_list=[radioButton_1, radioButton_2, radioButton_3, radioButton_4]
for rb in rb_list:
if radioButton_1.isChecked():
print("You choose number 1")
else radioButton_2.isChecked():
print("You choose number 2")
else radioButton_3.isChecked():
print("You choose number 3")
else radioButton_4.isChecked():
print("You choose number 4")我的代码正确吗?
发布于 2014-06-29 22:32:58
对于If语句所做的操作,您不需要For循环,因为您没有使用它。
你可以这样做:
def rb_check(self):
rb_list=[radioButton_1, radioButton_2, radioButton_3, radioButton_4]
for counter, rb in enumerate(rb_list):
if rb.isChecked():
print("You choose number {}".format(counter + 1))上面的代码假设按钮是按顺序排列的。如果你有用setObjectName函数命名的按钮,你可以这样做:
def rb_check(self):
rb_list=[radioButton_1, radioButton_2, radioButton_3, radioButton_4]
for rb in rb_list:
if rb.isChecked():
print("You choose {}".format(rb.objectName())) 希望这能有所帮助
https://stackoverflow.com/questions/24476887
复制相似问题