PyQt5:检查动态创建的复选框的存在并引用它们

时间:2018-07-18 15:29:40

标签: python python-3.x pyqt pyqt5

代码的基本布局

在这里,每当用户单击PushButton 'Press me'时,都会生成一个新的CheckBox。

from PyQt5 import QtWidgets, QtGui, QtCore

count = 1

class Window(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.vlayout = QtWidgets.QVBoxLayout()
        self.pushButton1 = QtWidgets.QPushButton("Press me", self)
        self.pushButton1.clicked.connect(self.addCheckbox(count))
        self.pushButton2 = QtWidgets.QPushButton("OK", self)
        self.vlayout.addWidget(self.pushButton1)
        self.vlayout.addWidget(self.pushButton2)

        self.setLayout(self.vlayout)

    def addCheckbox(self,count):
        global count
        self.vlayout.addWidget(str(count),QtWidgets.QCheckBox())
        count = count +1 

application = QtWidgets.QApplication(sys.argv)
window = Window()
window.setWindowTitle('Hello')
window.resize(250, 180)
window.show()
sys.exit(application.exec_())  

我想做什么?

现在您将拥有唯一的复选框,每个复选框具有不同的编号,我想添加更多功能。

每次用户选择特定的复选框时,我想知道用户单击PushButton OK后单击了哪个复选框。 例如:我点击复选框1-> OK-> print 1 on the screen

我该怎么做?

PS:我们需要考虑用户永远不会点击Press me的可能性,因此不会生成任何折叠框,并且会直接点击OK

1 个答案:

答案 0 :(得分:0)

只需使用列表来存储QCheckBox,然后通过迭代进行验证。

Modify Text File - Single File:   No Target Chosen
                      Status: ERROR
                      Additional Notes: ERROR - Unable to locate ASCII text file to be manipulated.  Deferring...

Modify Text File - Single File:   No Target Chosen
                      Status: ERROR
                      Additional Notes: ERROR - Unable to locate ASCII text file to be manipulated.  Deferring...

Install Action:           InstallAnywhere Variable
                      Status: SUCCESSFUL

Install Action:           InstallAnywhere Variable
                      Status: SUCCESSFUL

Check Disk Space:         /opt\EpsonJavaPOS
                      Status: SUCCESSFUL
                      Additional Notes: NOTE - Required Disk Space: 40,268,671; Free Disk Space: -1

Install Action:           Jump To: Next Unit Package Con't -- Prev: NO JUMP
                      Status: SUCCESSFUL

Modify Text File - Single File:   SetupPOS.properties
                      Status: ERROR
                      Additional Notes: ERROR - Unable to locate ASCII text file to be manipulated.  Deferring...