如何访问框架的要素,如果这些被放置在布局(HBoxLayout())
问题描述:
我创建一个窗口,让我当我按下一个按钮添加文本框一个阵列,但是当我按下按钮,调用函数,我得到一个错误,它说“IndicSelectWindow”对象有没有属性“无所谓”。我尝试了其他更简单的方法,例如更改标签所说的内容,但我也无法访问标签。我怎么能解决它?请... :(如何访问框架的要素,如果这些被放置在布局(HBoxLayout())
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_())
答
的问题造成的,因为这些变量只能在创建它们的上下文进行访问,在你的代码的情况下,构造函数。类的只有成员都可以访问的它全班我们必须把自身的变量的名称,例如:
layoutgrilla
到
self.layoutgrilla
你必须证明你的代码,很遗憾,我们不能预言家:P,我推荐阅读弗洛机翼来提高你的问题:我如何问一个很好的问题(https://*.com/help/how-to-ask) – eyllanesc
对不起,我忘了,但我得到了它。如果我能帮上忙,那就太好了。 –
对于当我解决问题,我建议你做[旅游](https://*.com/tour) – eyllanesc