【问题标题】:Kivy right click menuKivy 右键菜单
【发布时间】:2017-07-29 11:15:14
【问题描述】:

我试图找到一种在 Kivy 中启用常规右键单击的方法,但没有任何运气。

我可以找到一种方法来禁用多点触控:-

Config.set('input', 'mouse', 'mouse,disable_multitouch')

但是右击就像左击一样,我需要能够复制、剪切、粘贴等。

我正在制作某种信息中心 GUI。

【问题讨论】:

    标签: python python-2.7 user-interface kivy kivy-language


    【解决方案1】:

    检测右键

    您可以将on_touch_downif 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多