Kivy Clipboard.copy标签文本

时间:2016-09-23 07:40:57

标签: kivy kivy-language

当我双击标签时,我想复制Label:self.text的内容,但以下内容无效:

main.py

#!/usr/bin/kivy
# -*- coding: utf-8 -*-

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty


class DoubletapClipboardInterface(BoxLayout):
    pass


class DoubletapClipboardApp(App):
    #copy_clipboard = ObjectProperty()

    def build(self):
        self.title = 'DoubletapClipboard'

        #self.copy_clipboard = DoubletapClipboardInterface()
        return(DoubletapClipboardInterface())   # self.copy_clipboard


if __name__ == '__main__':
    DoubletapClipboardApp().run()

doubletapclipboard.kv

#:kivy 1.9.0
#:import Clipboard kivy.core.clipboard.Clipboard

<DoubletapClipboardInterface>:
    orientation: 'vertical'

    TextInput:
        hint_text: 'Try to paste here to see if it works'

    Label:
        text: 'Can I be copied?'
        on_double_tap: Clipboard.copy(self.text)  # <-- How do I do this the correct way?

错误

 kivy.lang.builder.BuilderException: Parser: File "/home/stef-ubuntu/bitbucket/kanjiorigin_data/test/doubletap_clipboard/doubletapclipboard.kv", line 11:
 ...
       9:    Label:
      10:        text: 'Can I be copied?'
 >>   11:        on_double_tap: Clipboard.copy(self.text)  # <-- How do I do this the correct way?
 ...
 AttributeError: double_tap
   File "/usr/lib/python3/dist-packages/kivy/lang/builder.py", line 628, in _apply_rule
     raise AttributeError(key)

2 个答案:

答案 0 :(得分:4)

关于@MatthiasSchreiber的建议我从TextInput()中复制了代码

main.py

#!/usr/bin/kivy
# -*- coding: utf-8 -*-

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.utils import platform

Clipboard = None
CutBuffer = None

class TouchLabel(Label):

    def __init__(self, **kwargs):
        self._touch_count = 0
        super(TouchLabel, self).__init__(**kwargs)
        self.register_event_type('on_double_tap')

        if platform == 'linux':
            self._ensure_clipboard()

    def _ensure_clipboard(self):
        global Clipboard, CutBuffer
        if not Clipboard:
            from kivy.core.clipboard import Clipboard, CutBuffer

    def on_touch_down(self, touch):
        print("hello")
        if self.disabled:
            return

        touch_pos = touch.pos
        if not self.collide_point(*touch_pos):
            return False
        if super(TouchLabel, self).on_touch_down(touch):
            return True

        touch.grab(self)
        self._touch_count += 1
        if touch.is_double_tap:
            self.dispatch('on_double_tap')

    def on_double_tap(self, *args):
        Clipboard.copy(self.text)  # <-- How do I do this the correct way?
        print("Copied :D")


class DoubletapClipboardInterface(BoxLayout):
    pass


class DoubletapClipboardApp(App):

    def build(self):
        self.title = 'DoubletapClipboard'

        return(DoubletapClipboardInterface())


if __name__ == '__main__':
    DoubletapClipboardApp().run()

doubletabclipboard.kv

#:kivy 1.9.0
# #:import Clipboard kivy.core.clipboard.Clipboard

<TouchLabel>

<DoubletapClipboardInterface>:
    orientation: 'vertical'

    TextInput:
        hint_text: 'Try to paste here to see if it works'

    TouchLabel:
        text: 'Can I be copied?'
        #on_double_tap: Clipboard.copy(self.text)  # <-- not working

答案 1 :(得分:2)

Label没有“on_double_tap”,您需要自己创建该方法。 虽然有一个TexInput,你可以看看它是如何在code中完成的。

此外,您需要将剪贴板导入到您的kv文件中。