如何访问框架的元素,如果这些元素放置在布局上 (HBoxLayout())
how to access the elements of a frame, if these are placed on layouts (HBoxLayout())
我正在创建一个 window,它允许我在按下按钮时添加一个文本框数组,但是当我按下按钮并调用该函数时,我收到一个错误消息,上面写着'IndicSelectWindow' 对象没有属性 "whatever "。我尝试其他更简单的方法,比如更改标签上的内容,但我也无法访问标签。我该怎么做才能修复它?请...:(
import sys
from PyQt4.QtGui import *
class IndicSelectWindow(QDialog):
def __init__(self, parent=None):
super(IndicSelectWindow, self).__init__(parent=parent)
# cero un layout vertical para las matrices
layoutmatrices = QVBoxLayout()
##########################
# create the area for the matrix
scrollarea = QScrollArea()
scrollarea.setWidgetResizable(True)
widgetdelscrollarea = QWidget()
layoutgrilla = QGridLayout(widgetdelscrollarea)
scrollarea.setWidget(widgetdelscrollarea)
###########################
# now now place elements in the array layout
#the elements consist of a layout where the buttons are placed and another layout where the area is placed
layoutelementosmatriz1 = QHBoxLayout()
labelm1 = QLabel("Matriz 1")
labelmf1 = QLabel("Filas")
labelmc1 = QLabel("Columnas")
botonm1 = QPushButton("Aceptar")
text_m1f = QLineEdit()
text_m1c = QLineEdit()
layoutelementosmatriz1.addWidget(labelm1)
layoutelementosmatriz1.addWidget(labelmf1)
layoutelementosmatriz1.addWidget(text_m1f)
layoutelementosmatriz1.addWidget(labelmc1)
layoutelementosmatriz1.addWidget(text_m1c)
layoutelementosmatriz1.addWidget(botonm1)
layoutelementosmatriz1.setSpacing(20)
botonm1.clicked.connect(lambda: self.create_matrix()) ##eventos
layoutmatrices.addLayout(layoutelementosmatriz1)
layoutmatrices.addWidget(scrollarea)
####################################3 i create the layout for the frame
layoutgeneral = QHBoxLayout()
self.setLayout(layoutgeneral)
self.resize(800, 500)
#################################
# I add the elements to the frame
layoutgeneral.addLayout(layoutmatrices)
###################################
def create_matrix(self): #this method I use to fill the array with "QLineEdit"
self.labelm1.setText("gdsgs")
for i in range(10):
for j in range(10):
self.layoutgrilla.addWidget(QLineEdit(), i, j) #line 18
if __name__ == '__main__':
app = QApplication(sys.argv)
w = IndicSelectWindow()
w.show()
sys.exit(app.exec_())
问题是因为变量只能在它们创建的上下文中访问,在您的代码构造函数的情况下。在整个class中只有classes的成员是可访问的,为此我们必须将self放在变量名中,例如:
layoutgrilla
到
self.layoutgrilla
我正在创建一个 window,它允许我在按下按钮时添加一个文本框数组,但是当我按下按钮并调用该函数时,我收到一个错误消息,上面写着'IndicSelectWindow' 对象没有属性 "whatever "。我尝试其他更简单的方法,比如更改标签上的内容,但我也无法访问标签。我该怎么做才能修复它?请...:(
import sys
from PyQt4.QtGui import *
class IndicSelectWindow(QDialog):
def __init__(self, parent=None):
super(IndicSelectWindow, self).__init__(parent=parent)
# cero un layout vertical para las matrices
layoutmatrices = QVBoxLayout()
##########################
# create the area for the matrix
scrollarea = QScrollArea()
scrollarea.setWidgetResizable(True)
widgetdelscrollarea = QWidget()
layoutgrilla = QGridLayout(widgetdelscrollarea)
scrollarea.setWidget(widgetdelscrollarea)
###########################
# now now place elements in the array layout
#the elements consist of a layout where the buttons are placed and another layout where the area is placed
layoutelementosmatriz1 = QHBoxLayout()
labelm1 = QLabel("Matriz 1")
labelmf1 = QLabel("Filas")
labelmc1 = QLabel("Columnas")
botonm1 = QPushButton("Aceptar")
text_m1f = QLineEdit()
text_m1c = QLineEdit()
layoutelementosmatriz1.addWidget(labelm1)
layoutelementosmatriz1.addWidget(labelmf1)
layoutelementosmatriz1.addWidget(text_m1f)
layoutelementosmatriz1.addWidget(labelmc1)
layoutelementosmatriz1.addWidget(text_m1c)
layoutelementosmatriz1.addWidget(botonm1)
layoutelementosmatriz1.setSpacing(20)
botonm1.clicked.connect(lambda: self.create_matrix()) ##eventos
layoutmatrices.addLayout(layoutelementosmatriz1)
layoutmatrices.addWidget(scrollarea)
####################################3 i create the layout for the frame
layoutgeneral = QHBoxLayout()
self.setLayout(layoutgeneral)
self.resize(800, 500)
#################################
# I add the elements to the frame
layoutgeneral.addLayout(layoutmatrices)
###################################
def create_matrix(self): #this method I use to fill the array with "QLineEdit"
self.labelm1.setText("gdsgs")
for i in range(10):
for j in range(10):
self.layoutgrilla.addWidget(QLineEdit(), i, j) #line 18
if __name__ == '__main__':
app = QApplication(sys.argv)
w = IndicSelectWindow()
w.show()
sys.exit(app.exec_())
问题是因为变量只能在它们创建的上下文中访问,在您的代码构造函数的情况下。在整个class中只有classes的成员是可访问的,为此我们必须将self放在变量名中,例如:
layoutgrilla
到
self.layoutgrilla