gpt4 book ai didi

Kivy Clipboard.copy 标签文本

转载 作者:行者123 更新时间:2023-12-05 00:17:38 25 4
gpt4 key购买 nike

我想在双击标签时复制 Label: self.text 的内容,但以下内容不起作用:

主文件

#!/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()

双击剪贴板.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)

最佳答案

根据@MatthiasSchreiber 的建议,我从 TextInput() 复制了代码

主文件

#!/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

关于Kivy Clipboard.copy 标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39655326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com