AttributeError:尝试将小部件添加到Layout的个性化ID时,“ super”对象没有属性“ __getattr__”

时间:2019-03-24 05:03:30

标签: python kivy

当我尝试将小部件(问题中的布局)添加到我修改为该布局的个性化ID时,会引发一个错误,我认为我无法识别个性化ID

gr_ly = GridLayout(id=i[1], rows=1)

self.LayoutGeneralCI.ids.realll.add_widget(gr_ly)

#Adding another widget to the custom id layout but just always puts me the widgets into the last layout executed

gr_ly.add_widget(self.DatosLayoutCI)

所有代码在这里

def selection_data_secciones(self):

    self.mainwid.dataBase = sqlite3.connect("UserData")
    self.mainwid.dataCursor = self.mainwid.dataBase.cursor()
    self.mainwid.dataCursor.execute("SELECT * FROM SECCIONES")
    fetch = self.mainwid.dataCursor.fetchall()
    for i in fetch:
        self.LayoutGeneralCI = LayoutGeneralCI(self.mainwid)  
        ref_idd = i[1]
        gr_ly = GridLayout(id=i[1], rows=1)
        print(type(gr_ly.id))
        self.LayoutGeneralCI.ids.realll.add_widget(gr_ly)
        print(self.LayoutGeneralCI.ids)
        self.LayoutGeneralCI.ids.title_sect_lbl.text = i[1]
        self.ids.container_ci.add_widget(self.LayoutGeneralCI) 
    for produ in self.mainwid.dataCursor.execute("SELECT * FROM MATERIALES"):
        self.DatosLayoutCI = DatosLayoutCI(self.mainwid)
        txtvar_ci = "Nombre: [b]{}[/b] \n".format(produ[1])
        txtvar2_ci = "Proveedor: [b]{}[/b] \n".format(produ[3])
        if produ[8] <= str(0):
            txtvar3_ci = "Disponibilidad: [color=#FF0000][b]Agotado[/b][/color]" 
        else:
            txtvar3_ci = "Disponibilidad: [color=#00FF00][b]Disponible[/b][/color]"
        txtvargeneral_ci = txtvar_ci + txtvar2_ci + txtvar3_ci
        self.DatosLayoutCI.ids.content_cill.text = txtvargeneral_ci
        var_sectttion = self.mainwid.AgregarProductos.ids.section_product.text#
        var_reference_id = produ[2]  
        gr_ly.add_widget(self.DatosLayoutCI)
    self.mainwid.dataBase.commit()   #DISCOMMENT NECCESARY
    self.mainwid.dataBase.close()

结果是:

self.LayoutGeneralCI.ids.var.add_widget(self.DatosLayoutCI)
File "kivy\properties.pyx", line 841, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

1 个答案:

答案 0 :(得分:0)

问题2

  

继续将DatosLayoutCI小部件添加到最后一个小部件gr_ly

解决方案

为了显示每个部分(SECCIONES)下的所有材料(材料),必须使用一个带有INNER JOIN的SQL语句或两个SELECT语句(嵌套的SELECT)。

代码段-嵌套的选择

def selection_data_secciones(self):

    self.mainwid.dataBase = sqlite3.connect("UserData")
    self.mainwid.dataCursor = self.mainwid.dataBase.cursor()
    self.mainwid.dataCursor.execute("SELECT * FROM SECCIONES")  # Sections
    fetch = self.mainwid.dataCursor.fetchall()

    for i in fetch:
        self.LayoutGeneralCI = LayoutGeneralCI(self.mainwid)
        ref_idd = i[1]
        gr_ly = GridLayout(id=i[1], rows=1)
        ...

        self.mainwid.dataCursor.execute("SELECT * FROM MATERIALES WHERE id=?", (gr_ly.id))
        materials = self.mainwid.dataCursor.fetchall()

        for produ in materials:
            self.DatosLayoutCI = DatosLayoutCI(self.mainwid)    # Datos = Data
            ...    
            gr_ly.add_widget(self.DatosLayoutCI)

    self.mainwid.dataBase.commit()   #DISCOMMENT NECCESARY
    self.mainwid.dataBase.close()

问题1

self.LayoutGeneralCI.ids.var.add_widget(self.DatosLayoutCI)
File "kivy\properties.pyx", line 841, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

原因

在Python脚本中创建的ID与在kv文件中创建的ID不同。

Kivy文档

Kv Language » Referencing Widgets

Kv Language » Accessing Widgets defined inside Kv lang in your python code

差异

kv文件

  • 为ID分配值时,请记住该值不是字符串。没有引号:好-> id: value,坏-> id: 'value'
  • 使用self.ids.realllself.ids['realll']以Python脚本访问它
  • 在解析您的kv文件时,kivy会收集所有标有id的小部件,并将它们放在此self.ids字典类型属性中。这意味着您还可以遍历这些小部件并访问它们的字典样式。

py文件

  • id是一个字符串
  • 无法使用self.ids.var
  • 访问它
  • 未存储在self.ids

解决方案

gr_ly = GridLayout(id=str(i[1]))

#####adding a widget to the personalized id of layout

gr_ly.add_widget(self.DatosLayoutCI)

self.LayoutGeneralCI.ids.realll.add_widget(gr_ly)

#######all the code is in a function so i want to créate a layout that will #######contain the widgets with a personalized id and then calling the #######personalized id to add the widget in different and specific layouts##

####defining the layout and it's personalized id and adding to the class

gr_ly = GridLayout(id=str(i[1]))

#####adding a widget to the personalized id of layout

gr_ly.add_widget(self.DatosLayoutCI)

self.LayoutGeneralCI.ids.realll.add_widget(gr_ly)#####1 PRIMERA OPCION