我试图在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'
我的问题是:
CreateCheckbox
课程或我的myChanges
方法是在错误的地方/我会把它们放在哪里?编辑:我想我发现了我做错了什么。在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_())
答案 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