Search code examples
pythonkivyright-click

right click menu from a button in kivy


I'm trying to get a right click menu to appear by right clicking a button, while still allowing left click to do something else.

Main.py

from kivy.config import Config
Config.set('input', 'mouse', 'mouse,disable_multitouch')

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.bubble import Bubble

class MainWidget(FloatLayout):

    def onpressed(self, widget, touch):
        if touch.button == 'left':
            print("LEFT")
        elif touch.button == 'right':
            print("RIGHT")
            ccp = CutCopyPaste
            self.add_widget(ccp)

class CutCopyPaste(Bubble):
    pass

class RightClickApp(App):
    pass

if __name__ == "__main__":
    RightClickApp().run()

rightclick.kv

MainWidget:

<MainWidget>:
    Button:
        text: "A"
        on_touch_down: root.onpressed

<CutCopyPaste>:
    size_hint: (None, None)
    size: (160, 160)
    pos_hint: {'center_x': .5, 'y': .6}
    BubbleButton:
        text: 'Cut'
    BubbleButton:
        text: 'Copy'
    BubbleButton:
        text: 'Paste'

All the ways I've found to do this haven't worked. Please help.


Solution

  • Two small problems with your code:

    1. Your onpressed() method is missing (). Just change ccp = CutCopyPaste to ccp = CutCopyPaste().
    2. You are also missing () in your kv file, as well as arguments to the onpressed() method. Just change on_touch_down: root.onpressed to on_touch_down: root.onpressed(*args).

    See the documentation concerning args in kv.