問題描述
我創建了一個 ScreenManager,并為該 ScreenManager 創建了幾個 Screen 實例.
I've created a ScreenManager, and created several Screen instances for that ScreenManager.
我希望每個屏幕都顯示一個 GridLayout 類.例如,假設您有:
I'd like each Screen to display a GridLayout class. For example, let's say you have:
class MainScreen(Screen):
...
class MainLayout(GridLayout):
...
當 MainScreen 是活動屏幕時,我希望顯示 MainLayout.
When MainScreen is the active screen, I'd like MainLayout to be shown.
有沒有辦法純粹在 python 中做到這一點(即沒有標記)?謝謝.
Is there a way to do this purely in python (i.e. without markup)? Thank you.
推薦答案
有沒有辦法純粹在 python 中做到這一點(即沒有標記)?謝謝.
Is there a way to do this purely in python (i.e. without markup)? Thank you.
您永遠不需要使用 kivy 語言(我假設這就是您所說的標記的意思),但強烈建議盡可能使用它,因為它使很多事情變得更容易.
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.
不過,要真正回答您的問題,您只需將 gridlayout 小部件添加到屏幕小部件,例如
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)
然后,當您在屏幕管理器中將當前屏幕設置為主屏幕時,您應該會看到 GridLayout.
Then when you set the current screen in your screenmanager to be mainscreen, you should see the GridLayout.
如果不清楚,這通常是您將小部件添加到其他小部件的方式.當你看到一個 kv 語言的例子時,比如
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
<MyScreen>:
GridLayout:
...
...最終,它被翻譯成與上面的示例代碼非常相似的內容 - 創建了一個 MyScreen 實例,并使用 add_widget 向其中添加了一個 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.
這篇關于將屏幕與 kivy 中的 GridLayout 類相關聯的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!