Kivy 右键菜单
Kivy right click menu
我试图找到一种在 Kivy 中启用常规右键单击的方法,但没有成功。
我可以找到一种禁用多点触控功能的方法:-
Config.set('input', 'mouse', 'mouse,disable_multitouch')
但是右键单击就像左键单击一样,我需要能够复制、剪切、粘贴等。
我正在制作一种信息中心 GUI。
正在检测右键单击
您可以结合使用 on_touch_down
和 if touch.button == 'right':
来检测右键单击。
获取上下文菜单
TextInput 有一个方法 _show_copy_paste 可以打开一个气泡作为上下文菜单。
我认为 Label 不可能做到这一点。如果您想实施它。我建议制作您自己的标签并启用这些属性并从 TextInput 中汲取灵感。
这项工作量很大。因此,我更喜欢将 TextInput 与 属性 readonly=True
一起使用。我编写了一个 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。
我试图找到一种在 Kivy 中启用常规右键单击的方法,但没有成功。
我可以找到一种禁用多点触控功能的方法:-
Config.set('input', 'mouse', 'mouse,disable_multitouch')
但是右键单击就像左键单击一样,我需要能够复制、剪切、粘贴等。
我正在制作一种信息中心 GUI。
正在检测右键单击
您可以结合使用 on_touch_down
和 if touch.button == 'right':
来检测右键单击。
获取上下文菜单
TextInput 有一个方法 _show_copy_paste 可以打开一个气泡作为上下文菜单。
我认为 Label 不可能做到这一点。如果您想实施它。我建议制作您自己的标签并启用这些属性并从 TextInput 中汲取灵感。
这项工作量很大。因此,我更喜欢将 TextInput 与 属性 readonly=True
一起使用。我编写了一个 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。