2014-09-24 117 views
0

在下面的代碼中,如果我刪除根窗口小部件中的on_touch_down函數,則按鈕是可按下的,但是當我添加on_touch_down函數時,不能再按下按鈕。我如何修復它,所以我不需要刪除on_touch_down函數來使按鈕可按下?Kivy:按鈕不再可按

from kivy.app import App 
from kivy.uix.widget import Widget 
from kivy.uix.button import Button 
from kivy.uix.floatlayout import FloatLayout 
from kivy.uix.boxlayout import BoxLayout 

class MenuScreen(BoxLayout): 

    def __init__(self,**kwargs): 
     super(MenuScreen,self).__init__(**kwargs) 
     self.add_widget(Button(
      text="Button1")) 
     self.add_widget(Button(
      text="Button2")) 
     self.add_widget(Button(
      text="Button3")) 
     self.add_widget(Button(
      text="Button4")) 
     self.add_widget(Button(
      text="Button5")) 

class RootWidget(FloatLayout): 
    def __init__(self,**kwargs): 
     super(RootWidget,self).__init__(**kwargs) 
     self.show_menu() 
    def show_menu(self): 
     self.add_widget(MenuScreen(
      size_hint=(0.5,0.8), 
      pos_hint={'center_x':0.5,'center_y':0.5}, 
      orientation="vertical")) 
     self.menuscreen=self.children[0] 

    def on_touch_down(self,touch): 
     if self.menuscreen.collide_point(touch.x,touch.y)!=True: 
      self.remove_widget(self.menuscreen) 

class MyApp(App): 
    def build(self): 
     app=RootWidget() 
     return app 

if __name__=="__main__": 
    MyApp().run() 

回答

1

您需要添加super(RootWidget, self).on_touch_down(touch)要調用的方法,它處理小部件的正常觸摸行爲的正常版本。實際上,您已用替換了正常的觸摸處理代碼,並帶有您自己的方法。

這是一個普通的python原理,如果這個想法對你來說是新的,你可以在它上面找到很多資源。