Kivy在文本输入中访问文本的问题将屏幕和屏幕管理器添加到程序

时间:2018-06-04 21:54:33

标签: python python-3.x kivy self kivy-language

在制作多屏幕应用程序之前没有任何问题,但现在我已经把它弄成了一个我收到错误:

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

每当我在列表对象中选择项目时按下删除按钮。

当我收到NoneType错误时,我认为我在某种程度上错误地引用了kivy层次结构中的对象,但我不确定如何。

我的相关代码是:

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)

我删除了不必要的代码,以便更容易阅读,但我的其他函数中的所有self. - 引用也都失败了,这也是我认为我错误地引用它们的部分原因。

1 个答案:

答案 0 :(得分:1)

您的错误是您在boxlayout中定义light_novel_text_input

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

您可能需要此功能,因为ObjectProperty属于LightNovel类:

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