Menu on long touch

cropleyb edited this page Oct 28, 2014 · 2 revisions

Summary

  • author: Gabriel Pettier
  • kivy: >= 1.1.1

A simple way to create a menu that shows when the user does a long touch.

Code

#!python
#!/usr/bin/env python
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from functools import partial

class Test(App):
    def create_clock(self, widget, touch, *args):
        callback = partial(self.menu, touch)
        Clock.schedule_once(callback, 2)
        touch.ud['event'] = callback

    def delete_clock(self, widget, touch, *args):
        Clock.unschedule(touch.ud['event'])

    def menu(self, touch, *args):
        menu = BoxLayout(
                size_hint=(None, None),
                orientation='vertical',
                center=touch.pos)
        menu.add_widget(Button(text='a'))
        menu.add_widget(Button(text='b'))
        close = Button(text='close')
        close.bind(on_release=partial(self.close_menu, menu))
        menu.add_widget(close)
        self.root.add_widget(menu)

    def close_menu(self, widget, *args):
        self.root.remove_widget(widget)

    def build(self):
        self.root = FloatLayout()
        self.root.bind(
            on_touch_down=self.create_clock,
            on_touch_up=self.delete_clock)

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

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.