我有10个以上的复选框。
我需要按用户列出已选中的复选框,因为每个复选框都有自己的功能,具体取决于复选的复选框数。!
答案 0 :(得分:0)
试一试:
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.listCheckBox = ["Checkbox_1", "Checkbox_2", "Checkbox_3", "Checkbox_4", "Checkbox_5",
"Checkbox_6", "Checkbox_7", "Checkbox_8", "Checkbox_9", "Checkbox_10" ]
self.listLabel = ['', '', '', '', '', '', '', '', '', '', ]
grid = QGridLayout()
for i, v in enumerate(self.listCheckBox):
self.listCheckBox[i] = QCheckBox(v)
self.listLabel[i] = QLabel()
grid.addWidget(self.listCheckBox[i], i, 0)
grid.addWidget(self.listLabel[i], i, 1)
self.button = QPushButton("Check CheckBox")
self.button.clicked.connect(self.checkboxChanged)
self.labelResult = QLabel()
grid.addWidget(self.button, 10, 0, 1,2)
grid.addWidget(self.labelResult,11, 0, 1,2)
self.setLayout(grid)
def checkboxChanged(self):
self.labelResult.setText("")
for i, v in enumerate(self.listCheckBox):
self.listLabel[i].setText("True" if v.checkState() else "False")
self.labelResult.setText("{}, {}".format(self.labelResult.text(),
self.listLabel[i].text()))
if __name__ == '__main__':
app = QApplication(sys.argv)
clock = Window()
clock.show()
sys.exit(app.exec_())
答案 1 :(得分:0)
感谢您的时间。
我以一种简单的方式解决它,
我列出一个清单
List = [(self.chckbox_name.isChecked() , "#name or string I need to be returned if condition is true") , ...etc ]
List_of_checked =[]
List_of_unchecked = []
for i, v in List:
if i == True:
List_of_checked.append (v)
else:
List_of_unchecked.append (v)