我在Kivy中使用一些回调方法创建了一个自定义Button
类,当我右键单击我希望能够从几个不同的操作中选择的按钮时:
from kivy.config import Congfig
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
from kivy.app import App
from kivy.uix import Button
class CustomButton(Button):
"""This is just for demonstration"""
def __init__(self, name='', **kwargs):
super(Service, self).__init__(**kwargs)
self.name = name
self.on_touch_down = self.mouse_click
def mouse_click(self, touch):
if self.collide_point(touch.x, touch.y):
if touch.button == 'right':
# Open context menu
pass
def callback_1(self):
# This will be called from one of the context options
pass
def callback_2(self):
# Or this will be called from a different option
pass
该代码仅用于演示目的,以表达我想要实现的目标。这可能吗?
提前致谢。
答案 0 :(得分:0)
在mouse_click()
方法(您有# Open context menu
)中,您可以调用自定义弹出式菜单,如:
self.popup = PopMenu(touch)
self.popup.open()
使用创建ModalView
的PopMenu类,类似于:
class PopMenu(object):
def __init__(self, touch):
myContent = BoxLayout(orientation='vertical')
button = Button(text='button1')
myContent.add_widget(button)
button = Button(text='button2')
myContent.add_widget(button)
button = Button(text='button3')
myContent.add_widget(button)
self.popup = ModalView(size_hint=(None, None), height=myContent.height, pos_hint={'x' : touch.spos[0], 'top' : touch.spos[1]})
self.popup.add_widget(myContent)
def open(self, *args):
self.popup.open()
您可以将回调添加到PopMenu中的按钮。