1

I'm trying to use a loop to create multiple Checkboxes. I'd like to be able to convert this grid of boxes to make a numpy array. The code manually creates a 5x5 matrix of checkboxes, corresponding to the same point in the numpy array. When this code is run and boxes are checked by the user, the numpy array alaways prints as such:

[[False False False False False]
 [False False False False False]
 [False False False False False]
 [False False False False False]
 [False False False False  True]]

I really don't know what's wrong.

import sys
from PyQt5.QtWidgets import (QWidget, QApplication, QPushButton, QCheckBox)
from PyQt5.QtCore import Qt
import numpy as np

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.grid =np.zeros([5,5], dtype=bool)
        self.x_pos, self.y_pos = 0, 0
        self.initUI()

    def initUI(self):
        for i in range(5):
            for j in range(5):
                self.x_pos, self.y_pos = i, j
                btn = QCheckBox(self)
                btn.move(50+17*i, 50+17*j)
                # btn.toggle()
                btn.stateChanged.connect(self.click)



        done = QPushButton('Done', self)
        done.clicked.connect(self._print)
        self.setGeometry(300, 300, 300, 200)
        self.show()

    def click(self, state):
        if state == Qt.Checked:
            self.grid[self.x_pos][self.y_pos] = True
        else:
            self.grid[self.x_pos][self.y_pos] = False


    def _print(self):
        print(self.grid)
1
  • self.x_pos and self.y_pos is changed while initUI() is running, but after that you don't change the variable. Therefore, those 2 variables stay [5,5] and only the element in that position changes. You should assign the number that can track down the position of actual checkbox to those 2 variables. Commented May 22, 2017 at 9:28

1 Answer 1

1

Main problem causing only your last element in array to change is you not assigning position after creating checkbuttons. Therefore it stays 5,5

One way to solve this problem is to assign checkbuttons into QGridLayout and renew every grid for every inputs. The code will be as follows :

from PyQt5.QtWidgets import QGridLayout

Under def __init__(self):

self.grid_layout = QGridLayout()
self.setLayout(self.grid_layout)

under def initUI(self):

for i in range(5):
    for j in range(5):
        btn = QCheckBox()
        self.grid_layout.addWidget(btn,i,j)
        btn.stateChanged.connect(self.click)

under def click(self,state):

if state == Qt.Checked :
    for i in range(5):
        for j in range(5):
            item = self.grid_layout.itemAtPosition(i,j)
            widget = item.widget()
            self.grid[i][j] = widget.isChecked()
else :
    pass
Sign up to request clarification or add additional context in comments.

7 Comments

I had originally thought of using a grid object, but it never returns the correct layout. I need an actual grid of checkboxes with very little space in between. Also, running your code throws the error: AttributeError: 'QWidgetItem' object has no attribute 'Widget'
theres' typo in the widget = item.Widget(). this should be widget = item.widget().
Still the same error. In any case, it still doesn't provide the output I'm looking for. I need a matrix of checkboxes. I tried using the built-in grid object, but it produced a grid with huge spaces inbetween the boxes, nothing seemed to change it, either. Edit: error in question NoneType' object has no attribute 'widget'
That's quite confusing because I am currently running same code as I've uploaded... Can you specify how the matrix is not showing up? Is it not in 5*5 matrix or not even showing?
The code will execute, but produce a window with 10 checkboxes all aligned vertically. If I click one, it automatically throws the error above without even clicking "done".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.