Repository navigation
|
Hey. When form a binding I create new widgets like: def action_foo(self):
table = DataTable()
self.mount(table)
table.focus()Is merely using I mean AFAIU Thanks, |
Replies: 3 comments 4 replies
|
A bit related perhaps, when should one fill widgets like I mean I could do this:
I guess I don't quite understand the concepts here. O:-) Should one fill in initial data before the mounting has happened or after? Should things like e.g. setting the focus happen from "within" the respective widget itself ( Thanks :-) |
|
1. Focusing a newly mounted widget
async def action_foo(self) -> None:
table = DataTable()
await self.mount(table)
table.focus()Your version without the 2. When to fill a
If loading is slow (I/O, a database), start it from 3. Where to set focus As a rule, the code that adds a widget decides whether it gets focus, since focus depends on context the widget itself doesn't know. The main exception is screens: for a screen's initial focus, use |
|
@pyrrhonic thanks for your help :-)
Is there any best practises when adding one's own attributes to such derived widget classes? I mean
But I assume only "static" ones?!
Thanks :-) |
1. Focusing a newly mounted widget
mount()doesn't mount the widget straight away. It schedules the mount and returns anAwaitMount. If you await it, the widget is fully mounted (it's in the DOM and has runon_mount) before your next line runs. Actions can beasync, so the usual way to write this is:Your version without the
awaitusually works too.Widget.focus()doesn't move focus immediately; it schedules the change withcall_later, which normally runs after the mount has finished. That depends on timing, though, which is probably where the glitch reports come from. Awaiting the mo…