PySide:创建复选框列表

PySide: Creating a list of checkboxes

我正在尝试在 PySide 中创建一个复选框列表。 这些复选框将位于框架内的网格内。

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

Ui_MainWindow class 之外,我有一个单独的 class 名为 CreateCheckbox,它试图在框架下创建一个复选框,设置对象的名称,添加它到网格中的一个点并设置它的文本。据我所知,它可以完美地执行前两个,问题出现在 self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1) 行。更具体地说,它有一个问题 grid 并抛出这个错误:AttributeError: 'CreateCheckbox' object has no attribute 'grid'

我的问题是:

  1. 我是不是用错了网格?
  2. 传入网格时是否不允许在网格周围使用点?
  3. 如何解决此问题,使复选框全部沿着网格的单个文件行向下移动?
  4. 我的 CreateCheckbox class 或 myChanges 方法是否放错了地方/我应该把它们放在哪里?

编辑:我想我发现我做错了什么。在 class CreateCheckbox 中,self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1) 中不应有 self.,因为网格不是 CreateCheckbox class

的实例

编辑 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_())

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

我认为您的意思是引用传递给 class 构造函数 (__init__)

grid 变量
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))

此代码使我相信您期望 CreateCheckBox 到 return 一个 QCheckBox 对象,但这不是您正在做的。

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

CreateCheckbox 应该是 return 一个 QCheckBox 对象的函数,或者是一个从 QCheckBox 派生的 class 函数。

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