Associating Screens with GridLayout classes in kivy

前端 未结 1 1355
离开以前
离开以前 2021-01-25 20:48

I\'ve created a ScreenManager, and created several Screen instances for that ScreenManager.

I\'d like each Screen to display a GridLayout class. For example, let\'s say

1条回答
  •  走了就别回头了
    2021-01-25 21:24

    Is there a way to do this purely in python (i.e. without markup)? Thank you.

    You never need to use kivy language (I assume that's what you mean by markup), though it's highly recommended where possible because it makes lots of stuff easier.

    Nonetheless, to actually answer your question, all you have to do is add your gridlayout widget to your screen widget, something like

    mainscreen = MainScreen()
    mainlayout = MainLayout()
    mainscreen.add_widget(mainlayout)
    

    Then when you set the current screen in your screenmanager to be mainscreen, you should see the GridLayout.

    Edit: In case it's unclear, this is in general the way you add widgets to other widgets. When you see an example in kv language like

    :
         GridLayout:
             ...
    

    ...ultimately that gets translated to something much like the above example code - an instance of MyScreen is created and a GridLayout is added to it with add_widget.

    0 讨论(0)
提交回复
热议问题