Kivy 在文本输入中访问文本的问题向程序添加屏幕和屏幕管理器
Issue in Kivy accessing text in a text input adding screens and screen manager to the program
在将其设为多屏应用程序之前没有任何问题,但现在我将其设为多屏应用程序时出现错误:
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.
引用也都失败了,这也是我认为我引用不正确的部分原因。
你的错误是你在boxlayout中定义了light_novel_text_input
。
<LightNovel>:
BoxLayout:
light_novel_text_input: searchbar
light_novel_list: saved
当你可能想要这个的时候,因为 ObjectProperty
s 属于 LightNovel
class:
<LightNovel>:
light_novel_text_input: searchbar
light_novel_list: saved
BoxLayout:
在将其设为多屏应用程序之前没有任何问题,但现在我将其设为多屏应用程序时出现错误:
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.
引用也都失败了,这也是我认为我引用不正确的部分原因。
你的错误是你在boxlayout中定义了light_novel_text_input
。
<LightNovel>:
BoxLayout:
light_novel_text_input: searchbar
light_novel_list: saved
当你可能想要这个的时候,因为 ObjectProperty
s 属于 LightNovel
class:
<LightNovel>:
light_novel_text_input: searchbar
light_novel_list: saved
BoxLayout: