首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Kivy TextInput on_double_tap

Kivy TextInput on_double_tap
EN

Stack Overflow用户
提问于 2013-10-26 00:49:43
回答 1查看 849关注 0票数 1

我已经添加了一个文本输入窗口到我的Kivy应用程序,我试图用窗口做两件事。默认情况下,文本输入窗口突出显示双击的单词。我想把这个单词存储到一个变量中,而不知道如何将它从输入窗口传递给变量。第二,我试图从操作系统中剪切并粘贴到Kivy中,但无法理解。任何帮助都将不胜感激。这是我到目前为止的代码。谢谢你帮助我走到这一步。

代码语言:javascript
代码运行次数:0
运行
复制
Builder.load_string('''

<MouseWidget>:
    image: image
    label: label
    orientation: 'vertical'
    Image:
        id: image
        source: root.source
    Label:
        id: label
        size_hint_y: None
        height: 50
        text: 'Test'
''')

class MouseWidget(BoxLayout):
    image = ObjectProperty()
    label = ObjectProperty()
    source = StringProperty()


    def on_touch_down(self, touch):
        if self.image.collide_point(*touch.pos):
            trigger = 0
            if touch.x >= 133 and touch.x <= 646 and touch.y >= 162 and touch.y <=675:
            self.label.text = str(touch.pos)


    def on_touch_up(self, touch):
        self.label.text = 'This is a test'



class TESTApp(App):
    def build(self):
        root = Accordion(orientation='horizontal')

        item= AccordionItem(title='Test')
        src = "image.png"
        image = MouseWidget(source=src, size_hint = (1.0, 1.0))

        textinput = TextInput(text='Hello world', size_hint = (0.5, 1.0))
        textinput.bind(text2 = on_double_tap())


        # add image to AccordionItem
        item.add_widget(image)
        item.add_widget(textinput)
        root.add_widget(item)

    return root

if __name__ == '__main__':
    TESTApp().run()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-26 08:54:50

只需像下面这样重写您的on_double_tap方法。

代码语言:javascript
代码运行次数:0
运行
复制
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.clock import Clock


class Test(TextInput):

    def on_double_tap(self):
        # make sure it performs it's original function
        super(Test, self).on_double_tap()

        def on_word_selection(*l):
            selected_word = self.selection_text
            print selected_word
            # do what you want with selected word here

        # let the word be selected wait for
        # next frame and get the selected word
        Clock.schedule_once(on_word_selection)

class TestApp(App):

    def build(self):
        return Test()


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

对于复制和粘贴TextInput支持ctrl + x、c、v,内部TextInput使用粘贴函数。您不应该直接使用它们,kivy负责处理这个问题,只需使用ctrl + c,x,v。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19601590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档