Translate

miércoles, 19 de agosto de 2015

Kivy - variable Label

Lately I have been working with Kivy to produce some simple Apps. One of the things I needed was a Label which text could be changed by double clicking on it. I came up with this derived class that merges Label, TextInput and Popup.

from kivy.app import App

from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput

class variableLabel (Label):

    def __init__(self, text):
        super(variableLabel, self).__init__()
        self.text  = text

    def on_touch_down(self, touch):
        if touch.is_double_tap and self.collide_point(*touch.pos):
          _input = TextInput(text=self.text, multiline=False, auto_dismiss=False)
          popup = Popup(title='Editing Label text', content=_input)
          _input.bind (text=self.on_text)
          _input.bind (on_text_validate=popup.dismiss)
          popup.open()

    def on_text (self, instance, value):
        self.text = value

class MyApp(App):

    def build(self):
        return variableLabel("New")

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

Do you have suggestions for improvement?