我只是试图制作一些我发现的示例代码,这些代码动态地将小部件添加到与屏幕管理器合并的视图中,我无法使其工作。
我找到了这个例子Associating Screens with GridLayout classes in kivy,据我所知,我已经实现了那里定义的策略,但我一直得到kivy.uix.screenmanager.ScreenManagerException:ScreenManager只接受屏幕小部件。
编辑:这是我的更新代码。现在得到错误:AttributeError:MainScreen实例没有属性' add_widget'
from kivy.uix.modalview import ModalView
from kivy.uix.listview import ListView
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.app import App
import citylists
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
#using 'sla'...whatever that means...
Builder.load_string("""
#:import ListItemButton kivy.uix.listview
#:import sla kivy.adapters.listadapter
<ListViewModal>:
ListView:
size_hint: .8, .8
adapter:
sla.ListAdapter(
data=["Item #{0}".format(i) for i in range(100)],
cls=ListItemButton.ListItemButton)
""")
class ListViewModal(ModalView):
def __init__(self, **kwargs):
super(ListViewModal, self).__init__(**kwargs)
class MainView(Screen):
def __init__(self, **kwargs):
kwargs['cols'] = 1
super(MainView, self).__init__(**kwargs)
listview_modal = ListViewModal()
self.add_widget(listview_modal)
class MainScreen():
pass
mainscreen=MainScreen()
mainlayout = MainView()
mainscreen.add_widget(mainlayout)
class CARApp(App):
screen_manager = None
def build(self):
self.screen_manager = ScreenManager()
self.screen_manager.add_widget(mainscreen)
if __name__ == '__main__':
CARApp().run()
答案 0 :(得分:1)
self.screen_manager.add_widget(MainScreen)
您正在传递实际的类MainScreen,但您需要添加该类的实例,即MainScreen()
。
编辑,虽然更多地查看代码,但您可能希望添加已创建的实例,即mainscreen
。将这个小部件创建移动到构建方法中也可能更简洁,因为它实际上需要它。