Kivy Clipboard.copy 标签文本
Posted
技术标签:
【中文标题】Kivy Clipboard.copy 标签文本【英文标题】:Kivy Clipboard.copy label text 【发布时间】:2017-01-31 23:49:30 【问题描述】:我想在双击标签时复制 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)
【问题讨论】:
【参考方案1】:根据@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
【讨论】:
【参考方案2】:标签没有“on_double_tap”,您需要自己创建该方法。 不过有一个用于 TexInput,您可以在 code 中查看它是如何完成的。
另外,您需要将剪贴板导入到您的 kv 文件中。
【讨论】:
以上是关于Kivy Clipboard.copy 标签文本的主要内容,如果未能解决你的问题,请参考以下文章