Обработка ввода с клавиатуры относительно источника в Kivy - PullRequest
0 голосов
/ 29 мая 2019

В настоящее время я немного экспериментирую с Kivy и добавил TextInput в мой графический интерфейс.

Теперь я хочу сохранить такие функции, как удаление последнего символа при нажатии клавиши Backspace и т. Д., Но также хочу, чтобы Kivy выполнял метод при вводе новой буквы в мой textInput.

Как бы я это сделал? Я попробовал следующий код, который работает с точки зрения вызова упомянутой функции, но не сохраняет забой и удаляет функциональность.

Код:

KV

#:kivy 1.9.0
<PageLayout>:
    GridLayout:

        searchterm: searchterm

        cols: 2
        rows: 2
        spacing: 5
        padding: 5

        Button:
            text: "maybe a listview of latest traces here"
            size_hint: .2,.5

        BoxLayout:
            orientation:'vertical'

            TextInput:
                id: searchterm
                multiLine: False
                hint_text: "Search for specific processes here"
                keyboard_on_key_down: root.on_search
        Button:
            text: "room for imagination"
            size_hint: 0.2, .5

        Button:
            text: "out of ideas"

    BoxLayout:
        orientation: "vertical"
        Button:
            text: "this is a new page for additional information"

        Label:
            halign: 'center'
            valign: 'center'
            size_hint: None, .025
            width: self.parent.width
            text: "About:"
            color: 0,0,0,1 #textcolor
            canvas.before:
                Color:
                    rgba: 0.5,0.5,0.5,1
                Rectangle:
                    pos: self.pos
                    size: self.size

        Label:
            halign: 'center'
            valign: 'center'
            size_hint: None, .025
            width: self.parent.width
            text: "This tool was written by me"
            color: 0,0,0,1 #textcolor
            canvas.before:
                Color:
                    rgba: 0.5,0.5,0.5,1
                Rectangle:
                    pos: self.pos
                    size: self.size

        Label:
            halign: 'center'
            valign: 'center'
            size_hint: None, .025
            width: self.parent.width
            text: "It is only meant for evaluation and testing purposes."
            color: 0,0,0,1 #textcolor
            canvas.before:
                Color:
                    rgba: 0.5,0.5,0.5,1
                Rectangle:
                    pos: self.pos
                    size: self.size

Python

import kivy

from kivy.app import App
from kivy.uix.pagelayout import PageLayout
from kivy.config import Config
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget

#WINDOW ATTRIBUTES
Config.set('graphics','resizable','1')
Config.set('graphics','width','1000')
Config.set('graphics','height','800')

class Visualizer(PageLayout):
    searchterm = ObjectProperty(None)
    def on_search(self,*args,**kwargs):
        print("Test")
        print(*args)
        print(**kwargs)

class VisualizerApp(App):


    def build(self):
        self.title = "MQTT IDS Visualizer. Author: Daniel"
        return Visualizer()



GLAPP = VisualizerApp()
GLAPP.run()

1 Ответ

1 голос
/ 30 мая 2019

Вам нужно позвонить на super для keyboard_on_key_down.Я считаю, что самый простой способ сделать это - определить подкласс TextInput примерно так:

class MyTextInput(TextInput):
    def keyboard_on_key_down(self, window, keycode, text, modifiers):
        App.get_running_app().root.on_search( window, keycode, text, modifiers)  # calls your `on_search()` method
        return super(MyTextInput, self).keyboard_on_key_down(window, keycode, text, modifiers)

Затем замените TextInput на MyTextInput в вашем файле kv.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...