C
C
Cockie2022-01-05 07:03:30
PyQt
Cockie, 2022-01-05 07:03:30

Why does the program just stop when I try to check a box or click a button?

import sys,time
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
from PyQt5.QtCore import QCoreApplication
global t
class category(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Категории')
        self.setGeometry(300, 300, 250, 150)
        qbtn = QPushButton('Quit', self)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(150, 100)
        qbtn.clicked.connect(self.close)
        self.j = ['bh','ghj','gggggg']
        self.z = []
        self.vlayout = QtWidgets.QVBoxLayout()
        for i in self.j:
            self.e = QtWidgets.QCheckBox(i)
            self.z.append(self.e)
            self.vlayout.addWidget(self.e)
        self.setLayout(self.vlayout)
        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_())

the actual code

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vsumin, 2022-01-05
@Vsumin

Hi.
error NameError: name 't' is not defined
see this section of code:

if toggle == QtCore.Qt.Checked:
            t.append(no)
        else:
            t.remove(no)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question