Button Long Press Event

105 views
Skip to first unread message

Shahadat Hossain

unread,
Aug 24, 2022, 4:14:48 PM8/24/22
to Kivy users support
Well I am trying to impalement a button which will change its text property on 5 second long press.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button


class Home(App):
    def build(self):
        home_container = BoxLayout(orientation = 'vertical')
       
        display = Label(text = '12345')
        counter_button = Button(text = 'Counter')
       
        home_container.add_widget(display)
        home_container.add_widget(counter_button)
       
        return home_container
       

if __name__ == '__main__':
    home = Home()
    home.run()

when the Counter button will be long pressed for 5 seconds the text property will be changed to Reset. And then the Reset button will be used to call a function which will reset the displayed number to 0.

My question is, does Kivy buttons support long press events?

Robert

unread,
Aug 24, 2022, 8:56:23 PM8/24/22
to Kivy users support

where you see you can get both the press and release events.
I expect all you need to add is a 5 second timer to test against. 

FYI on a mobile device a long press is typically about 0.4 sec, but that does not mean that is appropriate in your application.

Elliot Garbus

unread,
Aug 24, 2022, 11:06:27 PM8/24/22
to kivy-...@googlegroups.com

Here is an example that can easily be extended to do what you  are looking for.  The example is a little sophisticated registering a new event and dispatching that event.  This is how I would implement it to simplify use in kv, but it is not required.

 

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.clock import Clock

kv =
"""
BoxLayout:
    orientation: 'vertical'
    Button:
        text: 'Regular Button'
    FiveSecondButton:
        text: 'Five Second Button'
        on_press: self.text = 'Pressed'
        on_release: self.text = 'Five Second Button'
        on_five_second_press: self.text = 'Very Long Press'
"""


class FiveSecondButton(Button):
   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.schedule = None
       
self.register_event_type('on_five_second_press')

   
def on_five_second_press(self):
       
pass

    def
on_press(self):
       
self.schedule = Clock.schedule_once(self._long_press, 5)

   
def _long_press(self, _):
       
self.dispatch('on_five_second_press')

   
def on_release(self):
       
self.schedule.cancel()



class FiveSecondApp(App):
   
def build(self):
       
return Builder.load_string(kv)


FiveSecondApp().run()

--
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/aff473d2-5b8a-4a6b-a93e-8c5fca0a738fn%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages