mutual_dependent_props.pyΒΆ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
This example demonstrates how two mutually dependent props can be set
without getting into an infinite loop. Since flexx uses actions, this
is rather trivial.
"""

from flexx import event


class Temperature(event.Component):
    """ Temperature object with a settable prop for both Celcius and
    Fahrenheit.
    """

    c = event.FloatProp(doc='Temperature in degrees Celcius')
    f = event.FloatProp(doc='Temperature in degrees Fahrenheit')

    @event.action
    def set_c(self, t):
        t = float(t)
        self._mutate_c(t)
        self._mutate_f(t * 1.8 + 32)

    @event.action
    def set_f(self, t):
        t = float(t)
        self._mutate_f(t)
        self._mutate_c((t - 32) / 1.8)

    @event.reaction
    def on_temp_change(self):
        # This gets called once with two events when either C or F is changed.
        print('  temp in Celcius: %1.1f C' % self.c)
        print('  temp in Fahrenheit: %1.1f F' % self.f)

t = Temperature()

print('Water is freezing:')
t.set_c(0)
event.loop.iter()

print('Average annual temp in California')
t.set_f(59.4)
event.loop.iter()