您希望将按钮添加到 GridLayout 内的 Screen,而不是直接添加到 Screen。为此,请使用id 来引用GridLayout。
但是,您不能在__init__ 方法中使用id,因为在相应的小部件完全初始化之前不会应用kv 文件中定义的规则。 Ryan Pi 在这个问题中正确提供了解决方案:
Why can't I access the Screen.ids?
另一方面,稍后要更改屏幕,您必须在屏幕中定义 name 属性。
你的代码应该是:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.clock import Clock
kv_text='''\
<MyScreenManager>:
LandingScreen:
InputScreen:
<InputScreen@Screen>:
name: 'input_sc'
AnchorLayout:
id: anchor_1
<LandingScreen@Screen>:
name: 'landing_sc'
GridLayout:
id: grid_1
cols: 5
height: 480
width: 800
spacing: 25, 20
padding: 25,25
'''
class MyScreenManager(ScreenManager):
pass
class LandingScreen(Screen):
def __init__(self, **kwargs):
super(LandingScreen, self).__init__(**kwargs)
self.buttons = [] # add references to all buttons here
Clock.schedule_once(self._finish_init)
def _finish_init(self, dt):
self.ids.grid_1.cols = 5
# Loop to make 20 different buttons on screen
for x in range(15):
self.buttons.append(Button(text='button {}'.format(x)))
self.ids.grid_1.add_widget(self.buttons[x])
self.buttons[x].background_normal = 'YOUTUBE.png'
class MyKivyApp(App):
def build(self):
return MyScreenManager()
def main():
Builder.load_string(kv_text)
app = MyKivyApp()
app.run()
if __name__ == '__main__':
main()
如果您想使用Properties 而不是ids 方法(请参阅documentation),您应该执行以下操作:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.clock import Clock
from kivy.properties import ObjectProperty
kv_text='''\
<MyScreenManager>:
LandingScreen:
InputScreen:
<InputScreen@Screen>:
name: 'input_sc'
AnchorLayout:
id: anchor_1
<LandingScreen@Screen>:
name: 'landing_sc'
grid_1: grid_1
GridLayout:
id: grid_1
cols: 5
height: 480
width: 800
spacing: 25, 20
padding: 25,25
'''
class MyScreenManager(ScreenManager):
pass
class LandingScreen(Screen):
grid_1 = ObjectProperty(None) #<<<< Propertie
def __init__(self, **kwargs):
super(LandingScreen, self).__init__(**kwargs)
self.buttons = [] # add references to all buttons here
Clock.schedule_once(self._finish_init)
def _finish_init(self, dt):
self.grid_1.cols = 5
# Loop to make 20 different buttons on screen
for x in range(15):
self.buttons.append(Button(text='button{}'.format(x)))
self.grid_1.add_widget(self.buttons[x])
self.buttons[x].background_normal = 'star.png'
class MyKivyApp(App):
def build(self):
return MyScreenManager()
def main():
Builder.load_string(kv_text)
app = MyKivyApp()
app.run()
if __name__ == '__main__':
main()