2
我試圖將我的小部件創建轉移到.kv而不是在我的main.py中。我怎麼能仍然參考我的on_press命令?畫家是另一個孩子小部件,幷包含我想調用的功能,但我不確定如何從.kv中引用painter.acceptshape。Kivy - 使用.kv
class TestingApp(App):
def build(self):
parent = FloatLayout()
keepbtn = Button(pos= (10,10),text='Accept Shape',size_hint=(.25, .15),font_size=14, color=(0.960784, 1, 0.980392,1), background_normal = '',\
background_color= (0.0980392, 0.0980392, 0.439216,1), font_name='Exo2-Bold.otf')
restartbtn = Button(text='Restart',size_hint=(.2, .15),font_size=14, color=(0.960784, 1, 0.980392,1), background_normal = '',\
background_color= (0.0980392, 0.0980392, 0.439216,1), font_name='Exo2-Bold.otf')
renderbtn = Button(text = "Render Shape", size_hint=(.2, .15),font_size=14, color=(0.960784, 1, 0.980392,1), background_normal = '',\
background_color= (0.0980392, 0.0980392, 0.439216,1), font_name='Exo2-Bold.otf')
parent.add_widget(keepbtn)
parent.add_widget(restartbtn)
parent.add_widget(renderbtn)
# if free draw option
painter = FreeDraw()
keepbtn.bind(on_press=painter.acceptshape)
restartbtn.bind(on_press=painter.restart)
renderbtn.bind(on_press=painter.render)
parent.add_widget(painter)
# # if preloaded box
return parent
if __name__ == '__main__':
TestingApp().run()
KV:
<FloatLayout>:
canvas:
Rectangle:
source: 'background.jpg'
size: self.size
pos: self.pos
好!但是如果你仍然有疑問,那麼這可能會對你有所幫助。 http://stackoverflow.com/questions/30202801/how-to-access-id-widget-of-different-class-from-a-kivy-file-kv – kiok46