|
| 1 | +# Checkbox Component — PyUIkit |
| 2 | + |
| 3 | +A `Checkbox` in PyUIkit lets the user pick **one or more options** independently. |
| 4 | +It’s perfect for toggles, settings, and forms where multiple selections are allowed. |
| 5 | + |
| 6 | +Unlike radio buttons, **you can select multiple checkboxes at the same time**. |
| 7 | + |
| 8 | +> ⚠️ **Note:** Before using this component, it is recommended to read the [Quickstart Guide](https://github.com/Huzaifa-Atiq/PyUIkit/blob/main/documentation/Quickstart.md) if you haven't already to understand how to create windows, top-level Divs, and basic setup. |
| 9 | +
|
| 10 | +--- |
| 11 | + |
| 12 | +## How It Works |
| 13 | + |
| 14 | +* Each checkbox is **independent** and can be toggled on/off. |
| 15 | +* You can set a **default state** (checked or unchecked). |
| 16 | +* You can **read or change the state dynamically** with static methods. |
| 17 | +* Each checkbox is placed individually using `x` and `y` coordinates. |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## Parameters |
| 22 | + |
| 23 | +| Parameter | Type | Default | Description | |
| 24 | +| ------------ | ---- | --------- | -------------------------------------- | |
| 25 | +| `text` | str | "" | Label for the checkbox | |
| 26 | +| `x`, `y` | int | None | Position of the checkbox | |
| 27 | +| `id` | str | None | Unique ID to access this component | |
| 28 | +| `text_color` | str | `#ffffff` | Color of the label text | |
| 29 | +| `color` | str | `#00ff88` | Checkbox toggle color | |
| 30 | +| `font_size` | int | 14 | Label font size | |
| 31 | +| `default` | bool | False | Initial checked state (True = checked) | |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Static Methods |
| 36 | + |
| 37 | +| Method | Description | |
| 38 | +| -------------------------------------- | -------------------------------------------------------------------------------- | |
| 39 | +| `Checkbox.is_checked(id)` | Returns `True` if the checkbox is checked, otherwise `False` | |
| 40 | +| `Checkbox.set_checked(id, value=True)` | Programmatically sets the checkbox state (`True` = checked, `False` = unchecked) | |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +# Basic Usage |
| 45 | + |
| 46 | +A very simple example showing how to create a single checkbox. |
| 47 | + |
| 48 | +```python |
| 49 | +from pyuikit import Body, Div |
| 50 | +from pyuikit.components import Checkbox |
| 51 | + |
| 52 | +app = Body(title="Checkbox Example", width=400, height=250) |
| 53 | + |
| 54 | +Div( |
| 55 | + x=20, |
| 56 | + y=20, |
| 57 | + width=300, |
| 58 | + height=200, |
| 59 | + children=[ |
| 60 | + Checkbox( |
| 61 | + text="Accept Terms", |
| 62 | + id="termsCheckbox", |
| 63 | + x=10, |
| 64 | + y=10 |
| 65 | + ) |
| 66 | + ] |
| 67 | +) |
| 68 | + |
| 69 | +app.run() |
| 70 | +``` |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +# Intermediate Example — Multiple Checkboxes |
| 75 | + |
| 76 | +```python |
| 77 | +from pyuikit import Body, Div, Text |
| 78 | +from pyuikit.components import Checkbox |
| 79 | + |
| 80 | +app = Body(title="Multiple Checkbox Example", width=400, height=300) |
| 81 | + |
| 82 | +Div( |
| 83 | + x=20, |
| 84 | + y=20, |
| 85 | + width=350, |
| 86 | + height=250, |
| 87 | + children=[ |
| 88 | + Checkbox( |
| 89 | + text="Option A", |
| 90 | + id="optionA", |
| 91 | + x=10, |
| 92 | + y=10, |
| 93 | + default=True, |
| 94 | + text_color='#000000' |
| 95 | + ), |
| 96 | + Checkbox( |
| 97 | + text="Option B", |
| 98 | + id="optionB", |
| 99 | + x=10, |
| 100 | + y=50, |
| 101 | + text_color='#000000' |
| 102 | + ), |
| 103 | + Checkbox( |
| 104 | + text="Option C", |
| 105 | + id="optionC", |
| 106 | + x=10, |
| 107 | + y=90, |
| 108 | + text_color='#000000' |
| 109 | + ), |
| 110 | + Text( |
| 111 | + text="Check your choices", |
| 112 | + id="statusText", |
| 113 | + x=10, |
| 114 | + y=150, |
| 115 | + color="#ffd700", |
| 116 | + font_size=14, |
| 117 | + text_color='#000000' |
| 118 | + ) |
| 119 | + ] |
| 120 | +) |
| 121 | + |
| 122 | +app.run() |
| 123 | +``` |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +# Advanced Example — Dynamic Updates |
| 128 | + |
| 129 | +```python |
| 130 | +from pyuikit import Body, Div, Button, Text |
| 131 | +from pyuikit.components import Checkbox |
| 132 | + |
| 133 | +def show_selected(): |
| 134 | + checked = [] |
| 135 | + for cid in ["optionA", "optionB", "optionC"]: |
| 136 | + if Checkbox.is_checked(cid): |
| 137 | + checked.append(cid) |
| 138 | + Text.set_text("statusText", f"Checked: {', '.join(checked) if checked else 'None'}") |
| 139 | + |
| 140 | +def check_all(): |
| 141 | + for cid in ["optionA", "optionB", "optionC"]: |
| 142 | + Checkbox.set_checked(cid, True) |
| 143 | + |
| 144 | +app = Body(title="Checkbox Update Example", width=400, height=300) |
| 145 | + |
| 146 | +Div( |
| 147 | + x=20, |
| 148 | + y=20, |
| 149 | + width=350, |
| 150 | + height=250, |
| 151 | + children=[ |
| 152 | + Checkbox(text="Option A", id="optionA", x=10, y=10,text_color='#000000'), |
| 153 | + Checkbox(text="Option B", id="optionB", x=10, y=50,text_color='#000000'), |
| 154 | + Checkbox(text="Option C", id="optionC", x=10, y=90,text_color='#000000'), |
| 155 | + |
| 156 | + Button(text="Show Checked", x=10, y=140, on_click=show_selected), |
| 157 | + Button(text="Check All", x=150, y=140, on_click=check_all), |
| 158 | + |
| 159 | + Text(text="Waiting...", id="statusText", x=10, y=180, color="#ffd700", font_size=14) |
| 160 | + ] |
| 161 | +) |
| 162 | + |
| 163 | +app.run() |
| 164 | +``` |
| 165 | + |
0 commit comments