Answer the question
In order to leave comments, you need to log in
Where is the error in the checkbox layout?
there is a code, its task is to display a window with a line of buttons and checkboxes,
the problem is in checkboxes, I need the line with the "ok" button to be on top in one line
and the checkboxes are displayed from top to bottom 12, but my code does not work
import sys,time,math
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
from PyQt5.QtCore import QCoreApplication
class category(QtWidgets.QWidget):
def __init__(self):
global t
t =[]
super().__init__()
self.setWindowTitle('Категории')
self.setGeometry(300, 300, 800, 400)
qbtn = QPushButton('Quit', self)
qbtn.resize(qbtn.sizeHint())
qbtn.move(705, 370)
qbtn.clicked.connect(self.close)
okbtn = QPushButton('ок', self)
okbtn.resize(qbtn.sizeHint())
self.j = [] #тут наименования чекбоксов их может быть как меньше 12 так все 47
self.z = []
self.formLayout = QtWidgets.QFormLayout()
n = QtWidgets.QLineEdit()
self.formLayout.addRow("По сколько:", n)
self.vlayout = QtWidgets.QVBoxLayout()
self.vvlayout = QtWidgets.QVBoxLayout()
self.hlayout = QtWidgets.QHBoxLayout()
self.hhlayout = QtWidgets.QHBoxLayout()
self.hlayout.addLayout(self.formLayout)
self.hlayout.addWidget(okbtn)
self.vvlayout.addLayout(self.hlayout)
j =math.ceil(len(self.j)/12)
jj =len(self.j)%12
for ii in range(j):
if j == 1:
for i in self.j:
self.e = QtWidgets.QCheckBox(i)
self.z.append(self.e)
self.vlayout.addWidget(self.e)
if j > 1 and ii != j-1:
for i in range(12):
self.e = QtWidgets.QCheckBox(self.j[i])
self.z.append(self.e)
self.vlayout.addWidget(self.e)
else:
for i in range(jj):
self.e = QtWidgets.QCheckBox(self.j[i])
self.z.append(self.e)
self.vlayout.addWidget(self.e)
self.hhlayout.addLayout(self.vlayout)
self.vvlayout.addLayout(self.hhlayout)
self.setLayout(self.vvlayout)
for i in range(len(self.z)):
self.z[i].stateChanged.connect(
lambda state=self.z[i].isChecked(), d = self.j[i] :self.selectBooks(state, d))
def selectBooks(self, toggle, no):
if toggle == QtCore.Qt.Checked:
t.append(no)
else:
t.remove(no)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = category()
w.show()
sys.exit(app.exec_())
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question