Search code examples
pythonpython-3.xkivyselfkivy-language

Issue in Kivy accessing text in a text input adding screens and screen manager to the program


Prior to making it a multiple screen application there were no issues but now that I have made it one I get the error :

     if self.light_novel_list.adapter.selection:
 AttributeError: 'NoneType' object has no attribute 'adapter'

Whenever I press the delete button while selecting an item in my list object.

As I am getting a NoneType error I think that I am referring to the object in the kivy hierarchy incorrectly somehow but I am unsure how.

My relevant code is:

lightnovel.py:

import kivy
from urllib.request import Request, urlopen
from kivy.uix.screenmanager import ScreenManager, Screen
from bs4 import BeautifulSoup
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.listview import ListItemButton
from kivy.properties import StringProperty

class SavedListButton(ListItemButton):
    pass

class LightNovel(Screen):
    light_novel_text_input = ObjectProperty()
    light_novel_list = ObjectProperty()

    def delete(self):
        if self.light_novel_list.adapter.selection:

            selection = self.light_novel_list.adapter.selection[0].text
            self.light_novel_list.adapter.data.remove(selection)
            self.light_novel_list._trigger_reset_populate()

class Series(Screen):
    pass




class LightNovelApp(App):
    def build(self):
        screen_manager = ScreenManager()
        screen_manager.add_widget(LightNovel(name="screen_one"))
        screen_manager.add_widget(Series(name="screen_two"))
        return screen_manager

lnApp = LightNovelApp()
lnApp.run()

lightnovel.kv:

#: import main lightnovel
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton


<LightNovel>:
    BoxLayout:
        light_novel_text_input: searchbar
        light_novel_list: saved
        orientation: "vertical"
        id: main
        BoxLayout:
            id: secondbar
            size_hint: 1, .1
            Button:
                id: delete
                size_hint: .5, 1
                text: "Delete"
                on_press: root.delete()
            Button:
                id: goto
                size_hint: .5, 1
                text: "Go to"
                on_press: 
                    root.goto()
                    root.manager.transition.direction = 'left'
                    root.manager.transition.duration = 1
                    root.manager.current = 'screen_two'

        ListView:
            id: saved
            adapter:
                ListAdapter(data=["test"], cls=main.SavedListButton)

I have cut out unnecessary code so that it is easier to read but all of my self.-references in my other functions also fail which is part of why I think I am referencing them incorrectly.


Solution

  • Your error is that you define light_novel_text_input in the boxlayout.

    <LightNovel>:
        BoxLayout:
            light_novel_text_input: searchbar
            light_novel_list: saved
    

    When you might want this, because the ObjectPropertys belongs to the LightNovel class:

    <LightNovel>:
        light_novel_text_input: searchbar
        light_novel_list: saved
        BoxLayout: