【问题标题】:AttributeError: 'super' object has no attribute '__getattr__' when trying to add a widget to a personalized id of LayoutAttributeError: 'super' object has no attribute '__getattr__' 在尝试将小部件添加到 Layout 的个性化 id 时
【发布时间】:2019-03-24 05:03:30
【问题描述】:

当尝试将小部件(问题中的布局)添加到我修改为布局的个性化 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__'

【问题讨论】:

  • 你必须在这个问题中添加更多代码,其他问题将来会自动排除,所以未来的读者不会理解你的问题,想法是你的问题不仅会为你服务,而且现在和未来的社区

标签: python kivy


【解决方案1】:

问题 2

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

解决方案

为了显示每个部分 (SECCIONES) 下的所有材料 (MATERIALES),必须使用一个带有 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'
  • 在 Python 脚本中使用self.ids.realllself.ids['realll'] 访问它
  • 当您的 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

【讨论】:

  • 我尝试了那个解决方案,但结果是一样的,具体的问题是我想添加到个性化布局小部件中,它与布局具有相同的 id,但是当我运行它时尝试这个代码总是在执行的最终布局上插入布局,另一个建议,请帮助
  • 如果要添加两个gr_ly,则必须在实例化每个gr_ly 后执行gr_ly.add_widget(self.DatosLayoutCI)
  • 我添加了更多代码以更好地理解问题,请检查一下,希望您能找到答案!谢谢
  • 请给我一个答案
  • 男人们!你太棒了!
猜你喜欢
  • 2023-01-22
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2022-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-01
相关资源
最近更新 更多