Rmor91
Rmor91

Reputation: 262

Kivy: buttons no longer pressable

In the following code, if I remove the on_touch_down function in the root widget, the buttons are pressable but when I add the on_touch_down function, the buttons can no longer be pressed. How do I fix it so I don't need to remove the on_touch_down function to make the buttons pressable?

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()

Upvotes: 0

Views: 62

Answers (1)

inclement
inclement

Reputation: 29488

You need to add super(RootWidget, self).on_touch_down(touch) to call the normal version of the method, which handles the widget's normal touch behaviour. As it is, you've replaced the normal touch-handling code with your own method.

This is a normal python principle, you can find lots of resources on it if the idea is new to you.

Upvotes: 1

Related Questions