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

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

class MyApp(App):

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

if __name__ == '__main__':

Do you have suggestions for improvement?

viernes, 16 de enero de 2015


Binary numbers to fractions

The mapping from binary numbers to fractions described in this article can be written in GNU Octave as the function

bin2q = @(x) arrayfun (@str2num,x) (2.^-[1:length(x)].')

Giving the results that .0100... and .001111... map to 1/4

octave> bin2q ('0100000000000000000')
ans =  0.25000
octave> bin2q ('0011111111111111111')
ans =  0.25000

Números binarios a fracciones

El mapeo de números binario a fracciones, descriptoi en este artículo se puede definir como la siguiente función en GNU Octave

bin2q = @(x) arrayfun (@str2num,x) (2.^-[1:length(x)].')

Dando el resultado que .0100... y .001111... se mapean en 1/4

octave> bin2q ('0100000000000000000')
ans =  0.25000
octave> bin2q ('0011111111111111111')
ans =  0.25000