Skip to content

Commit

Permalink
Touch-up widget guide byte example.
Browse files Browse the repository at this point in the history
See #3913.
  • Loading branch information
rodrigogiraoserrao committed Dec 21, 2023
1 parent ff70df8 commit 70585d3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
11 changes: 7 additions & 4 deletions docs/examples/guide/compound/byte02.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, bit: int, value: bool) -> None:
self.bit = bit
self.value = value

value = reactive(0) # (1)!
value = reactive(False) # (1)!

def __init__(self, bit: int) -> None:
self.bit = bit
Expand Down Expand Up @@ -90,9 +90,12 @@ def compose(self) -> ComposeResult:

def on_bit_switch_bit_changed(self, event: BitSwitch.BitChanged) -> None:
"""When a switch changes, update the value."""
value = 0
for switch in self.query(BitSwitch):
value |= switch.value << switch.bit
current_value = int(self.query_one(Input).value or "0")
value_change = 1 << event.bit
if event.value: # Bit changed from 0 to 1.
value = current_value + value_change
else: # Bit changed from 1 to 0.
value = current_value - value_change
self.query_one(Input).value = str(value)


Expand Down
11 changes: 7 additions & 4 deletions docs/examples/guide/compound/byte03.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, bit: int, value: bool) -> None:
self.bit = bit
self.value = value

value = reactive(0)
value = reactive(False)

def __init__(self, bit: int) -> None:
self.bit = bit
Expand Down Expand Up @@ -101,9 +101,12 @@ def compose(self) -> ComposeResult:

def on_bit_switch_bit_changed(self, event: BitSwitch.BitChanged) -> None:
"""When a switch changes, update the value."""
value = 0
for switch in self.query(BitSwitch):
value |= switch.value << switch.bit
current_value = int(self.query_one(Input).value or "0")
value_change = 1 << event.bit
if event.value: # Bit changed from 0 to 1.
value = current_value + value_change
else: # Bit changed from 1 to 0.
value = current_value - value_change
self.query_one(Input).value = str(value)

def on_input_changed(self, event: Input.Changed) -> None: # (3)!
Expand Down

0 comments on commit 70585d3

Please sign in to comment.