Skip to content

Commit 569569a

Browse files
Complete the basic material on traitsui.
1 parent 2a01a78 commit 569569a

1 file changed

Lines changed: 108 additions & 19 deletions

File tree

slides/03_traitsui.md

Lines changed: 108 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jupyter:
55
extension: .md
66
format_name: markdown
77
format_version: '1.3'
8-
jupytext_version: 1.13.7
8+
jupytext_version: 1.14.0
99
kernelspec:
1010
display_name: Python 3 (ipykernel)
1111
language: python
@@ -39,7 +39,7 @@ jupyter:
3939

4040
- Just declare what needs to be done
4141
- Do not need to write a lot of code
42-
- Embed 2D plots with `chaco`
42+
- Embed 2D plots with `matplotlib` or `chaco`
4343
- Embed 3D plots with `mayavi`
4444
- Build rich scientific dialogs
4545

@@ -82,12 +82,13 @@ jupyter:
8282
<!-- #endregion -->
8383

8484
```python
85-
from traits.api import HasStrictTraits, Int, Str, Enum
85+
from traits.api import HasStrictTraits, Int, Str, Enum, Bool
8686

8787
class Person(HasStrictTraits):
88-
name = Str()
89-
age = Int()
88+
name = Str
89+
age = Int
9090
handedness = Enum('left', 'right')
91+
drinks = Bool(False)
9192

9293
```
9394

@@ -111,7 +112,8 @@ from traitsui.api import Item, View
111112
view1 = View(
112113
Item(name='name', style='readonly'),
113114
Item(name='age'),
114-
Item(name=handedness', visible_when='age > 10'),
115+
Item(name='handedness'),
116+
Item(name='drinks', visible_when='age >= 18'),
115117
)
116118
```
117119

@@ -156,16 +158,16 @@ p.edit_traits(view=view1)
156158
from traitsui.api import Group
157159

158160
class Person(HasStrictTraits):
159-
name = Str()
160-
age = Int()
161-
gender = Enum('female', 'male', 'other')
161+
name = Str
162+
age = Int
163+
handedness = Enum('left', 'right')
162164

163165
traits_view = View(
164166
Group(
165167
Item(name='name'),
166168
Item(name='age'),
167-
Item(name='gender'),
168-
label='Personnel profile',
169+
Item(name='handedness'),
170+
label='Person profile',
169171
show_border=True
170172
)
171173
)
@@ -184,10 +186,10 @@ p.edit_traits()
184186
## View attributes
185187

186188
- `dock`: `{'fixed', 'horizontal', 'vertical', 'tabbed'}`
187-
- `height`/`width`
189+
- `height`/`width`: int
188190
- `icon`/`image`
189-
- `resizable`
190-
- `scrollable`
191+
- `resizable`: bool
192+
- `scrollable`: bool
191193
- `title`: name of the window
192194
- `buttons`
193195
- `key_bindings`
@@ -203,16 +205,17 @@ p.edit_traits()
203205
from traitsui.api import CancelButton, OKButton
204206

205207
class Person(HasStrictTraits):
206-
name = Str()
207-
age = Int()
208-
gender = Enum('female', 'male', 'other')
208+
name = Str
209+
age = Int
210+
likes_queso = Bool
211+
handedness = Enum('left', 'right')
209212

210213
traits_view = View(
211214
Group(
212215
Item(name='name'),
213216
Item(name='age'),
214-
Item(name='gender'),
215-
label='Personnel profile',
217+
Item(name='handedness'),
218+
label='Person profile',
216219
show_border=True,
217220
),
218221
buttons=[OKButton, CancelButton]
@@ -225,6 +228,92 @@ p = Person(name='Worf', age=20)
225228
p.edit_traits()
226229
```
227230

231+
<!-- #region slideshow={"slide_type": "slide"} -->
232+
## Specifying an editor
233+
234+
- We illustrate the powerful `InstanceEditor` here
235+
- Consider the following
236+
237+
<!-- #endregion -->
238+
239+
```python
240+
from traits.api import Instance
241+
242+
class Person(HasStrictTraits):
243+
name = Str
244+
age = Int
245+
handedness = Enum('left', 'right')
246+
bff = Instance('Person') # Notice the quotes.
247+
248+
traits_view = View(
249+
Group(
250+
Item(name='name'),
251+
Item(name='age'),
252+
Item(name='handedness'),
253+
Item(name='bff', style='custom'),
254+
label='Person profile',
255+
)
256+
)
257+
```
258+
259+
```python
260+
frodo = Person(name='Frodo', age=30)
261+
sam = Person(name='Sam', age=29, bff=frodo)
262+
```
263+
```python
264+
sam.edit_traits()
265+
```
266+
267+
<!-- #region slideshow={"slide_type": "slide"} -->
268+
## Discussion
269+
270+
- Note the embedding
271+
- Implicitly uses an InstanceEditor
272+
- Can configure the view it uses if needed
273+
274+
<!-- #endregion -->
275+
276+
```python
277+
from traitsui.api import InstanceEditor
278+
279+
bff_view = View(Group(
280+
Item(name='name'),
281+
Item(name='age'),
282+
Item(name='handedness'),
283+
label='BFF',
284+
)
285+
)
286+
```
287+
288+
```python slideshow={"slide_type": "slide"}
289+
class Person(HasStrictTraits):
290+
name = Str
291+
age = Int
292+
handedness = Enum('left', 'right')
293+
bff = Instance('Person')
294+
295+
traits_view = View(
296+
Group(
297+
Item(name='name'),
298+
Item(name='age'),
299+
Item(name='handedness'),
300+
Item(name='bff', style='custom', show_label=False,
301+
editor=InstanceEditor(view=bff_view)),
302+
label='Person profile',
303+
)
304+
)
305+
```
306+
307+
308+
```python
309+
frodo = Person(name='Frodo', age=30)
310+
sam = Person(name='Sam', age=29, bff=frodo)
311+
```
312+
```python
313+
sam.edit_traits()
314+
```
315+
316+
228317

229318
<!-- #region slideshow={"slide_type": "slide"} -->
230319
## Toolkit selection

0 commit comments

Comments
 (0)