Search code examples
pythonpython-2.7user-interfacekivykivy-language

Kivy right click menu


I was trying to find a way to enable regular right click in Kivy, but without any luck.

I could find a way to disable the multitouch thing with:-

Config.set('input', 'mouse', 'mouse,disable_multitouch')

But then the right click works just like left click, I need to be able to copy, cut, paste, etc..

I am making sort of an Information center GUI.


Solution

  • enter image description here

    Detecting right click

    You can use the on_touch_down in combination with if touch.button == 'right': to detect a right click.

    Getting a context menu

    TextInput has a method _show_copy_paste which opens up a Bubble as a context menu.

    I do not think this is possible with Label. If you would like to implement it. I would suggest making your own label with these properties enabled and taking ideas from TextInput.

    This is quite a lot of work. I would, therefore, prefer using TextInput with the property readonly=True. I have coded a version of TextInput which opens the Contextmenu aka Bubbles when right clicked. This is implemented in the sample app below. I coded and tested it on windows.

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.config import Config
    from kivy.base import EventLoop
    from kivy.uix.textinput import TextInput
    
    
    Config.set('input', 'mouse', 'mouse,disable_multitouch')
    
    
    
    class RightClickTextInput(TextInput):   
    
        def on_touch_down(self, touch):
    
            super(RightClickTextInput,self).on_touch_down(touch)
    
            if touch.button == 'right':
                print("right mouse clicked")
                pos = super(RightClickTextInput,self).to_local(*self._long_touch_pos, relative=True)
    
                self._show_cut_copy_paste(
                    pos, EventLoop.window, mode='paste')
    
    
    kv_string = Builder.load_string("""
    RightClickTextInput:
        use_bubble: True
        text: ('Palimm'*10+"\\n")*40
        multiline: True
        #readonly: True
    """)
    
    
    
    class MyApp(App):
        def build(self):
            return kv_string
    
    if __name__ == '__main__':
        MyApp().run()
    

    Kivy has mobile devices in mind. If you are not doing anything with touch it might be worth checking out tkinter.