2017-06-14 104 views
4

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

因为我需要百余复选框,我认为这将是最好的这些复选框存储在列表中。 在class Ui_MainWindow(object):def setupUi(self, MainWindow):和内我打电话我的方法myChangesself.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方法?

编辑:我想我发现我做错了什么。在class CreateCheckbox,应该有self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1)没有self.,因为网格是不是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_()) 

回答

1

错误是告诉你,CreateCheckbox没有成员命名的网格。

我认为你的意思是引用您传递到类的构造函数(__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

是啊,你说得对。是否有任何理由让我做'self.grid = grid'而不是网格。addWidget ...等? –

+0

是的,如果你打算在'CreateCheckbox .__ init__'之外的任何地方引用网格对象。 – shrewmouse

1

此代码使我相信,你希望CreateCheckBox返回一个QCheckBox对象grid变量,但是这不是什么你在做。

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 
+2

你是对的,我完全忘了返回复选框 –