|
from kivy.lang import Builder
from kivymd.toast import toast
from kivymd.uix.bottomsheet import MDListBottomSheet
from kivymd.app import MDApp
KV = '''
MDScreen:
MDToolbar:
title: "Example BottomSheet"
pos_hint: {"top": 1}
elevation: 10
MDRaisedButton:
text: "Open list bottom sheet"
on_release: app.show_example_list_bottom_sheet()
pos_hint: {"center_x": .5, "center_y": .5}
'''
class Example(MDApp):
def build(self):
return Builder.load_string(KV)
def callback_for_menu_items(self, *args):
toast(args[0])
def show_example_list_bottom_sheet(self):
bottom_sheet_menu = MDListBottomSheet()
for i in range(1, 11):
bottom_sheet_menu.add_item(
f"Standart Item {i}", lambda x, y=i: self.callback_for_menu_items(f"Standart Item {y}"),
)
bottom_sheet_menu.open()
Example().run()
-----------------------------------------------------------------各位大佬, 这是kivymd官方BottomSheet例程,我把回调函数的参数{y}改成{i}, 结果所有按钮响应的是 Standart Item 10, 也就是for循环i最终的值, 这令我十分困惑,有大佬可以指点一下吗?
|
|