Python Kivy中的问题访问文本输入中的文本向程序添加屏幕和屏幕管理器
在将其制作为多屏幕应用程序之前,没有出现任何问题,但现在我已将其制作为多屏幕应用程序,因此出现了以下错误:Python Kivy中的问题访问文本输入中的文本向程序添加屏幕和屏幕管理器,python,python-3.x,kivy,self,kivy-language,Python,Python 3.x,Kivy,Self,Kivy Language,在将其制作为多屏幕应用程序之前,没有出现任何问题,但现在我已将其制作为多屏幕应用程序,因此出现了以下错误: if self.light_novel_list.adapter.selection: AttributeError: 'NoneType' object has no attribute 'adapter' 每当我在选择列表对象中的项目时按delete按钮时 由于我得到了一个非类型错误,我认为我在某种程度上错误地引用了kivy层次结构中的对象,但我不确定如何引用 我的相关代
if self.light_novel_list.adapter.selection:
AttributeError: 'NoneType' object has no attribute 'adapter'
每当我在选择列表对象中的项目时按delete按钮时
由于我得到了一个非类型错误,我认为我在某种程度上错误地引用了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)
我删去了不必要的代码,以便更容易阅读,但我在其他函数中的所有自我引用也会失败,这也是我认为我引用错误的部分原因。您的错误是在boxlayout中定义了light\u novel\u text\u输入
<LightNovel>:
BoxLayout:
light_novel_text_input: searchbar
light_novel_list: saved
谢谢你,我很确定这是件愚蠢的事情,在过去的两天里我一直在努力想办法解决它。祝你今天愉快,再次感谢你。
<LightNovel>:
light_novel_text_input: searchbar
light_novel_list: saved
BoxLayout: