|
1 | | -# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE |
| 1 | +# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE |
2 | 2 |
|
3 | 3 | import ltk |
4 | 4 |
|
| 5 | +DELIVERY_OPTIONS = ["1-day", "2-day", "pickup"] |
| 6 | + |
5 | 7 | class Product(ltk.Model): |
6 | | - """ This is a model with two fields """ |
7 | | - name: str = "" |
8 | | - price: float = 0.0 |
| 8 | + name: str = "Wrench" |
| 9 | + price: float = 50.0 |
| 10 | + count: int = 10 |
| 11 | + warranty: bool = False |
| 12 | + service: bool = True |
| 13 | + delivery: int = 1 |
| 14 | + summary: str = "" |
9 | 15 |
|
10 | | -product = Product() |
| 16 | + def changed(self, name, value): |
| 17 | + if name == "summary": |
| 18 | + return |
| 19 | + self.summary = f""" |
| 20 | + {self.count} * {self.name} = |
| 21 | + ${round(self.count * self.price):,} |
| 22 | + {'including warranty' if self.warranty else ''} |
| 23 | + {'with service' if self.service else ''} |
| 24 | + - Delivery: {DELIVERY_OPTIONS[self.delivery]} |
| 25 | + """ |
11 | 26 |
|
12 | | -def clear(event): |
13 | | - product.name = "" |
| 27 | +product = Product() |
| 28 | +product.count = 60 |
14 | 29 |
|
15 | 30 | def create(): |
| 31 | + form = ltk.VBox( |
| 32 | + row( |
| 33 | + "Name:", |
| 34 | + ltk.Input(product.name).width(300) |
| 35 | + ), |
| 36 | + row( |
| 37 | + "Price:", |
| 38 | + ltk.Input(product.price).width(300) |
| 39 | + .attr("type", "number") |
| 40 | + ), |
| 41 | + row( |
| 42 | + "Count:", |
| 43 | + ltk.Slider(product.count, 0, 100).width(250) |
| 44 | + .css("margin-top", 10), |
| 45 | + ltk.Label(product.count) |
| 46 | + .css("margin", "5px 15px") |
| 47 | + ), |
| 48 | + row( |
| 49 | + "Warranty:", |
| 50 | + ltk.Checkbox(product.warranty) |
| 51 | + .css("width", 18) |
| 52 | + ), |
| 53 | + row( |
| 54 | + "Delivery:", |
| 55 | + ltk.Select(DELIVERY_OPTIONS, product.delivery) |
| 56 | + .css("margin-top", 2) |
| 57 | + .height(24) |
| 58 | + ), |
| 59 | + row( |
| 60 | + "Service:", |
| 61 | + ltk.Switch("With white gloves:", product.service) |
| 62 | + ), |
| 63 | + ltk.Div("<hr>"), |
| 64 | + row( |
| 65 | + "Summary:", |
| 66 | + ltk.Text(product.summary) |
| 67 | + ) |
| 68 | + ) |
16 | 69 | return ( |
17 | 70 | ltk.VBox( |
18 | 71 | ltk.Heading2("LTK Model-View Demo"), |
19 | | - |
20 | | - ltk.Label("Product Name (as ltk.Input):"), |
21 | | - ltk.Input(product.name) |
22 | | - .attr("placeholder", "Please enter a name") |
23 | | - .css("border", "2px solid red"), |
24 | | - |
25 | | - ltk.Break(), |
26 | | - |
27 | | - ltk.Label("Product Name (as ltk.Text):"), |
28 | | - ltk.Text(product.name) |
| 72 | + form |
29 | 73 | .css("border", "2px solid green") |
30 | | - .css("height", 34), |
31 | | - |
32 | | - ltk.Break(), |
33 | | - ltk.Button("Clear product.name", ltk.proxy(clear)), |
| 74 | + .css("padding", 12) |
| 75 | + .css("font-size", 24) |
| 76 | + .attr("name", "MVC"), |
| 77 | + ltk.Button("Buy Hammers", order_hammers) |
| 78 | + .css("margin-top", 24) |
| 79 | + .css("border-radius", 8) |
| 80 | + .css("padding", 12) |
34 | 81 | ) |
35 | | - .css("font-size", 24) |
| 82 | + .attr("id", "mvc") |
36 | 83 | .attr("name", "MVC") |
37 | 84 | ) |
| 85 | + |
| 86 | +def order_hammers(event): |
| 87 | + product.name = "Hammer" |
| 88 | + product.count = 10 |
| 89 | + product.price = 100.0 |
| 90 | + product.warranty = True |
| 91 | + product.delivery = 2 |
| 92 | + product.service = False |
| 93 | + |
| 94 | +def row(label, *widgets): |
| 95 | + return ltk.HBox( |
| 96 | + ltk.Text(label) |
| 97 | + .css("font-size", 16) |
| 98 | + .height(33) |
| 99 | + .width(120), |
| 100 | + *widgets |
| 101 | + ) |
| 102 | + |
0 commit comments