4

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

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

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

我的问题是:

  1. 我是否以错误的方式使用网格?
  2. 当我传递它时,我不允许在网格周围使用点吗?
  3. 我该如何解决这个问题,使复选框都沿着网格的单个文件行向下移动?
  4. 我的CreateCheckbox班级或我的myChanges方法是否在错误的地方/我应该把它们放在哪里?

编辑:我想我发现我做错了什么。in class CreateCheckbox,应该没有self.inself.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1)因为网格不是CreateCheckbox类的实例

编辑 2:以防万一有人想让文本正常工作,请在周围加上引号MainWindowself.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_())
4

2 回答 2

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
于 2017-06-14T14:19:51.927 回答
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))
于 2017-06-14T13:53:39.263 回答