Coustom on_mypress on_myrelease

11 views
Skip to first unread message

Degenerate Tech

unread,
Sep 18, 2020, 7:15:52 AM9/18/20
to Kivy users support
i have an Widget ....how to define my own on_mypress on_myrelease ,such that when i click it will do some thing ....but i dont want to use Buttonbehaviour ...
please give an easy example using on_touch_down and on_touch_up...
Message has been deleted

Degenerate Tech

unread,
Sep 18, 2020, 8:28:17 AM9/18/20
to Kivy users support
######code below is working but  when i passed cb=CustomBtn(on_test=self.btn_pressed)  click not taking ....please tell me how to do this ...
####These core level work will make me more flexible in kivy

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ListProperty
from kivy.event import EventDispatcher



class RootWidget(BoxLayout):

 def __init__(self, **kwargs):
     super(RootWidget, self).__init__(**kwargs)
     self.add_widget(Button(text='btn 1'))
     cb = CustomBtn()
     cb.bind(on_test=self.btn_pressed)
     self.add_widget(cb)
     self.add_widget(Button(text='btn 2'))

 def btn_pressed(self, *arg):
     print ('clicked ',arg)





class CustomBtn(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.register_event_type('on_test')

def on_touch_up(self, touch):
if self.collide_point(*touch.pos):
self.dispatch('on_test')
return True
return super(CustomBtn, self).on_touch_down(touch)
def on_test(self, *arg):pass





class TestApp(App):

 def build(self):
     return RootWidget()


if __name__ == '__main__':
 TestApp().run() 

Elliot Garbus

unread,
Sep 18, 2020, 2:07:14 PM9/18/20
to kivy-...@googlegroups.com

You could do something like this… but doing it with the bind is correct.

 

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty


class RootWidget(BoxLayout):

   
def __init__(self, **kwargs):
       
super(RootWidget, self).__init__(**kwargs)
       
self.add_widget(Button(text='btn 1'
))
        cb = CustomBtn(
touch_action=self.btn_pressed)
       
# cb.bind(on_test=self.btn_pressed)
       
self.add_widget(cb)
       
self.add_widget(Button(text='btn 2'))

   
def btn_pressed(self, *arg):
       
print('clicked ', *arg)


class CustomBtn(Widget):
    touch_action = ObjectProperty(
None, allownone=True)

   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.register_event_type('on_test')

   
def on_touch_up(self, touch):
       
if self.collide_point(*touch.pos):
           
self.dispatch('on_test')
           
return True
        return
super(CustomBtn, self).on_touch_down(touch)

   
def on_test(self, *args):
       
print('on_test fired')
       
if self.touch_action:
           
self.touch_action(*args)


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/ffa9346c-5c6e-419d-9adb-aaf026348b39o%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages