PySide:创建一个复选框列表

时间:2017-06-14 13:33:19

标签: python checkbox pyqt pyside qgridlayout

我试图在PySide中创建一个复选框列表。 这些复选框将驻留在框架内的网格中。

因为我需要超过一百个复选框,我认为最好将这些复选框存储在列表中。 在class Ui_MainWindow(object):def setupUi(self, MainWindow):,我可以使用myChanges调用我的方法self.myChanges(MainWindow, self.customCheckBoxes, self.frame, self.gridLayout)。在我的上方,我创建一个空列表,尝试使用self.customCheckBoxes = []

存储对象

Ui_MainWindow类之外,我有一个名为CreateCheckbox的单独类,它尝试在框架下创建一个复选框,设置对象的名称,将其添加到网格并设置它的文本。从我所知道的,它可以完美地执行前两个,问题由行self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1)引起。更具体地说,它在grid时出现问题并引发此错误:AttributeError: 'CreateCheckbox' object has no attribute 'grid'

我的问题是:

  1. 我是以错误的方式使用网格吗?
  2. 我通过时不允许在网格周围使用点?
  3. 如何解决此问题,以便所有复选框都沿网格向下单个文件行?
  4. 我的CreateCheckbox课程或我的myChanges方法是在错误的地方/我会把它们放在哪里?
  5. 编辑:我想我发现了我做错了什么。在class CreateCheckbox中,self.中应该没有self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1),因为网格不是CreateCheckbox类的实例

    编辑2:万一有人想让文字正常工作,请在MainWindow周围加上引号 self.checkBox.setText(QtGui.QApplication.translate(MainWindow, text, None, QtGui.QApplication.UnicodeUTF8))所以你有 self.checkBox.setText(QtGui.QApplication.translate("MainWindow", text, None, QtGui.QApplication.UnicodeUTF8))

    这里是完整的代码:

    from PySide import QtCore, QtGui
    
    
    class Ui_MainWindow(object):
        def myChanges(self, MainWindow, checkboxes, frame, grid):
            for j in range(100):
                checkboxes.append(CreateCheckbox(MainWindow, frame, grid, "Test", j))
    
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(800, 600)
            self.centralwidget = QtGui.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.frame = QtGui.QFrame(self.centralwidget)
            self.frame.setGeometry(QtCore.QRect(180, 90, 371, 311))
            self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
            self.frame.setFrameShadow(QtGui.QFrame.Raised)
            self.frame.setObjectName("frame")
            self.gridLayout_2 = QtGui.QGridLayout(self.frame)
            self.gridLayout_2.setObjectName("gridLayout_2")
            self.gridLayout = QtGui.QGridLayout()
            self.gridLayout.setObjectName("gridLayout")
            self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtGui.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtGui.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
            # Create list holding checkbox objects
            self.customCheckBoxes = []
            self.myChanges(MainWindow, self.customCheckBoxes, self.frame, self.gridLayout)
    
        def retranslateUi(self, MainWindow):
            MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
    
    
    class CreateCheckbox(object):
        def __init__(self, MainWindow, frame, grid, text, gridPlace):
            # 1. Create under appropriate frame
            self.checkBox = QtGui.QCheckBox(frame)
            # 2. Set its name
            #    Although the designer does this, pretty sure this is unneccesary
            self.checkBox.setObjectName(chr(gridPlace))
            # 3. Add it to the appropriate spot in the grid
            self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1)
            # 4. Set text that user sees
            # For now, I'm just sending 'Test'
            self.checkBox.setText(QtGui.QApplication.translate(MainWindow, text, None, QtGui.QApplication.UnicodeUTF8))
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        MainWindow = QtGui.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
    

2 个答案:

答案 0 :(得分:1)

错误告诉您CreateCheckbox没有名为grid的成员。

我认为您的意思是引用传递给类构造函数的grid变量(__init__

class CreateCheckbox(object):
    def __init__(self, MainWindow, frame, grid, text, gridPlace):
        #################
        self.grid = grid
        #################

        # 1. Create under appropriate frame
        self.checkBox = QtGui.QCheckBox(frame)
        # 2. Set its name
        #    Although the designer does this, pretty sure this is unneccesary
        self.checkBox.setObjectName(chr(gridPlace))
        # 3. Add it to the appropriate spot in the grid
        self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1)
        # 4. Set text that user sees
        # For now, I'm just sending 'Test'
        self.checkBox.setText(QtGui.QApplication.translate(MainWindow, text, None, QtGui.QApplication.UnicodeUTF8))

答案 1 :(得分:1)

此代码让我相信您希望CreateCheckBox返回QCheckBox个对象,但这不是您正在做的事情。

def myChanges(self, MainWindow, checkboxes, frame, grid):
    for j in range(100):
        checkboxes.append(CreateCheckbox(MainWindow, frame, grid, "Test", j))

CreateCheckbox应该是一个返回QCheckBox对象的函数或一个派生自QCheckBox的类。

def CreateCheckbox(MainWindow, frame, grid, text, gridPlace):
        # 1. Create under appropriate frame
        checkBox = QtGui.QCheckBox(frame)
        # 2. Set its name
        #    Although the designer does this, pretty sure this is unneccesary
        checkBox.setObjectName(chr(gridPlace))
        # 3. Add it to the appropriate spot in the grid
        grid.addWidget(checkBox, gridPlace, 1, 1, 1)
        # 4. Set text that user sees
        # For now, I'm just sending 'Test'
        checkBox.setText(QtGui.QApplication.translate(MainWindow, text, None, QtGui.QApplication.UnicodeUTF8))
        return checkBox

或者您可以将CreateCheckbox设为方法:

class Ui_MainWindow(object):
    def CreateCheckbox(self, MainWindow, frame, grid, text, gridPlace):
        # 1. Create under appropriate frame
        checkBox = QtGui.QCheckBox(frame)
        # 2. Set its name
        #    Although the designer does this, pretty sure this is unneccesary
        checkBox.setObjectName(chr(gridPlace))
        # 3. Add it to the appropriate spot in the grid
        grid.addWidget(checkBox, gridPlace, 1, 1, 1)
        # 4. Set text that user sees
        # For now, I'm just sending 'Test'
        checkBox.setText(QtGui.QApplication.translate(MainWindow, text, None, QtGui.QApplication.UnicodeUTF8))
        return checkBox