【发布时间】:2022-01-24 06:19:30
【问题描述】:
当从布局中添加/删除对象时,如何使用可重用方法为对象设置动画
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.factory import Factory
kv='''
<Image_1@BoxLayout>:
orientation:'vertical'
#id:img_1
Image:
source:"/storage/emulated/0/Download/download (37).jpeg"
Button:
text:"remove"
on_press: self.parent.remove()
BoxLayout:
orientation:'vertical'
GridLayout:
cols:1
id:sc_grid
Button:
size_hint:None,None
text:"add"
on_press:
app.Add()
'''
class MyApp(MDApp):
def build(self):
return Builder.load_string(kv)
def Add(self):
Image=Factory.Image_1()
Image.remove = lambda: self.root.ids.sc_grid.remove_widget(Image)
self.root.ids.sc_grid.add_widget(Image)
MyApp().run()
在上面的代码中,Add 方法将工厂对象 Image1 添加到布局中
【问题讨论】: