1

Kivy APP にテキスト入力ウィンドウを追加しました。ウィンドウで 2 つのことをしようとしています。デフォルトでは、テキスト入力ウィンドウは、ダブルクリックされた単語を強調表示します。その単語を変数に保存したいのですが、入力ウィンドウから変数に渡す方法がわかりません。次に、OS から Kivy にカット アンド ペーストしようとしていますが、それがわかりません。どんな助けでも大歓迎です。ここに私がこれまでに持っているコードがあります。ここまで私を助けてくれた Inclement に感謝します。

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()
4

1 に答える 1

2

on_double_tap以下のようにメソッドをオーバーライドするだけです。

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 は_cut、_copy、および _paste 関数を使用します。それらを直接使用する必要はありません.kivyがこれを処理します.ctrl + c、x、vを使用するだけです.

于 2013-10-26T08:54:50.503 に答える