Kivy AttributeError:'super'对象没有属性'__getattr __'

问题描述 投票:0回答:1

我有以下问题:我想要一个屏幕,显示从数据库中获取的记录列表。该列表应允许用户单击一个条目,然后切换到显示该条目的所有详细信息的“结果”屏幕。

我的主屏幕中的browseButton应该切换到BrowseWindow屏幕,同时,在其中构建与所获取的db条目相对应的按钮列表(这是makeList函数的作用,方法是调用WMan()。dbFetch())。

到目前为止。现在,我想将on_release事件绑定到这些按钮,以便单击它们将当前屏幕切换到ResultWindow并在此打印输出(这由WMan()。printRecipe()执行)。但是,这会触发错误:

     self.ids.result.ids.title.text = recipeName                           
   File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

我希望我能够足够清楚地解释我的问题。这是我的kv文件:

WMan:
    MainWindow:
    BrowseWindow:
    ResultWindow:
        id: result

<MainWindow>:
    name: 'main'
    browseButton: browseButton

    BoxLayout:
        Button:
            text: 'browse'
            on_release:
                app.root.current = 'browse'
                app.root.current_screen.makeList()

        Button:
            id: browseButton
            text: 'result'
            on_release:
                app.root.current = 'result'

<BrowseWindow>:
    name: 'browse'
    on_enter: root.bindList()
    browseList: browseList

    BoxLayout:
        id: browseList
        orientation: 'vertical'
        size_hint: .9, None
        pos_hint: {"x": .05, "top": .8}
        #height: self.minimum_height


<ResultWindow>:
    name: 'result'
    on_enter: app.root.printRecipe('test')

    Label:
        id: title
        pos_hint: {"x":0.05, "top": .9}
        size_hint: (.9, None)
        height: self.texture_size[0]

    ScrollView:
        id: scroll
        size_hint: (.9, .75)
        pos_hint: {"x": 0.05, "top": .75}
        StackLayout:
            size_hint: .9, None
            orientation: 'lr-tb'
            spacing: 0, 2
            height: self.minimum_height
            Label:
                text: "Ingredients:"
                size_hint: 1, None
                #text_size: self.size
                #halign: 'left'
                height: '30sp'
            BoxLayout:
                id: ingredientsBox
                orientation: 'vertical'
                size_hint: 1, None
                height: self.minimum_height

py脚本:

from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder


class MainWindow(Screen):
    pass

class BrowseWindow(Screen):

    def makeList(self):
        recipesList = WMan().dbFetch()
        for i in recipesList:
            self.browseList.add_widget(
                Button(
                    text=i,
                    height='30sp'))
        return self.browseList

    def bindList(self):
        for child in self.browseList.children:
            print(child.text)
            child.bind(on_release = self.listBindings(child)) 

    def listBindings(self, i):
        self.manager.current = 'result'
        WMan().printRecipe(i.text)

class ResultWindow(Screen):
    pass    

class WMan(ScreenManager):

    def dbFetch(self):
        recipesList = ['Recipe1', 'Recipe2', 'Recipe3', 'Recipe4']
        return recipesList   

    def searchRecipe(self, nameInput):
        recipeName = nameInput
        ingsList = ['Lorem', 'Ipsum', 'Dolor']
        steps = "Sed at tortor condimentum, congue nibh sit amet, ornare ligula. "
        return recipeName, ingsList, steps

    def printRecipe(self, nameInput):
        recipeName, ingsList, steps = self.searchRecipe(nameInput)

        self.ids.result.ids.title.text = recipeName                           

        for i in range(0, len(ingsList)):
            self.current_screen.ids.ingredientsBox.add_widget(
                Label(
                    text=ingsList[i]))


kv = Builder.load_file("test.kv")

class TestApp(App):
    def build(self):
        return kv

if __name__ == "__main__":
    TestApp().run()

我有以下问题:我想要一个屏幕,显示从数据库中获取的记录列表。此列表应允许用户单击条目,然后切换到“结果”屏幕...

python kivy
1个回答
0
投票

问题是,在您使用WMan()的任何地方,您都在创建WMan的新实例,该实例与GUI中的内容无关。并且由于在显示ids时分配了WMan,因此ids实例中没有WMan不会显示。

© www.soinside.com 2019 - 2024. All rights reserved.