我使用循环在一个 kivy 文件中创建了 5 个按钮。 当我尝试打印单击按钮的文本时,我总是创建最后一个。所以我会索引它们。有什么建议吗?谢谢
py文件:
from kivy.app import App
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.lang import Builder
class Screen1(Screen):
pass
class app(App):
values = ('one', 'two', 'three', 'four', 'five')
def build(self):
sm=ScreenManager()
sm.add_widget(Screen(name='screen_1'))
return Builder.load_file('lab.kv')
def print_that(self, i):
print(i)
app().run()
kv文件:
#:import Button kivy.uix.button.Button
ScreenManager:
Screen:
name:'screen1'
BoxLayout:
on_parent:
[self.add_widget(Button(text=i,on_press=lambda x:app.print_that(i))) for i in app.values]