Skip to content
Snippets Groups Projects
Select Git revision
  • 2255eefba417acfadd1e740e44c44766e1de719d
  • master default protected
2 results

example1.py

Blame
  • example1.py 1.21 KiB
    from math import inf
    
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.properties import NumericProperty, StringProperty
    
    
    class Field(BoxLayout):
        label_text = StringProperty()
    
    
    class Example1App(App):
        smallest_positive = NumericProperty(inf)
    
        def add_field(self):
            container = self.root.ids.fields
            container.add_widget(Field(label_text=f'Field #{len(container.children)}: '))
    
        def remove_field(self):
            container = self.root.ids.fields
            if len(container.children) > 0:
                container.remove_widget(container.children[0])
    
        @staticmethod
        def _find_smallest_positive(values):
            smallest_positive = inf
            for value in values:
                if 0 < value < smallest_positive:
                    smallest_positive = value
            return smallest_positive
    
        def update(self):
            container = self.root.ids.fields
            values = []
            for field in container.children:
                try:
                    values.append(float(field.ids.input.text))
                except ValueError:
                    pass
            self.smallest_positive = Example1App._find_smallest_positive(values)
    
    
    if __name__ == '__main__':
        app = Example1App()
        app.run()