Reputation: 6342
I want to copy the content of a Label: self.text when I double tap the label, but the following is not working:
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)
Upvotes: 4
Views: 2326
Reputation: 6342
On suggestion of @MatthiasSchreiber I copied the code from 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
Upvotes: 4
Reputation: 2527
There is no "on_double_tap" for a Label, you need to create that method yourself. There is one for TexInput though and you could look how it's done there in the code.
Also, you need to import the Clipboard into your kv file.
Upvotes: 2