I'm trying to get the button to call a function, in the kivy file I could do it with having *args in the brackets. How do I get the funciton to work in the python file.
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.button import Button
from kivy.uix.bubble import Bubble
class MainWidget(FloatLayout):
clicked = False
def __init__(self, **kwargs):
b = Button(text="A")
b.bind(self.onpressed)
def onpressed(self, widget, touch):
if touch.button == 'left':
print("LEFT")
if self.clicked:
self.remove_widget(self.ccp)
self.clicked = False
elif touch.button == 'right':
print("RIGHT")
if self.clicked:
self.remove_widget(self.ccp)
self.ccp = CutCopyPaste()
self.add_widget(self.ccp)
self.clicked = True
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(*args)
<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.
If you want the Button
instance and the touch
object to be passed to your onpressed()
method, then bind it to on_touch_down
, which provides those parameters. Like this:
def __init__(self, **kwargs):
super().__init__(**kwargs) # added missing call
b = Button(text="A")
b.bind(on_touch_down = self.onpressed)
self.add_widget(b) # added
Also, you must call the super()
__init__()
method whenever you over-ride it. And your code was not adding the Button
to the MainWidget
.