检测右键
您可以将on_touch_down 与if touch.button == 'right': 结合使用来检测右键单击。
获取上下文菜单
TextInput 有一个方法 _show_copy_paste 可以打开一个 Bubble 作为上下文菜单。
我认为这对于 Label 是不可能的。如果你想实现它。我建议在启用这些属性的情况下制作您自己的标签,并从 TextInput 中汲取灵感。
这是一项相当多的工作。因此,我更喜欢使用带有属性readonly=True 的TextInput。我编写了一个 TextInput 版本,它在右键单击时打开 Contextmenu aka Bubbles。这是在下面的示例应用程序中实现的。我在 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 考虑到了移动设备。如果你没有用触摸做任何事情,那么可能值得一试 tkinter。