Skip to content

Context

管理 Flet 控件的上下文,包括页面引用和自动更新行为。

可通过 context 访问该上下文实例。

只读属性 ​

名称类型默认值说明
page'Page'—Returns the current Page associated with the context. Example: python # take page width anywhere in the app width = ft.context.page.width Returns: The current page. Raises: RuntimeError: If the property is accessed outside a running Flet app.

方法 ​

名称类型默认值说明
enable_auto_update——Enables auto-update behavior for the current context. Example: python import flet as ft # disable auto-update globally for the app ft.context.disable_auto_update() def main(page: ft.Page): # enable auto-update just inside main ft.context.enable_auto_update() page.controls.append(ft.Text("Hello, world!")) # page.update() - we don't need to call it explicitly ft.run(main)
disable_auto_update——Disables auto-update behavior for the current context. Example: python import flet as ft def main(page: ft.Page): def button_click(): ft.context.disable_auto_update() b.content = "Button clicked!" # update just the button b.update() page.controls.append(ft.Text("This won't appear")) # no page.update() will be called here page.controls.append(b := ft.Button("Action!", on_click=button_click)) # page.update() - auto-update is enabled by default ft.run(main)
enable_components_mode——Enables components mode in the current context.
disable_components_mode——Disables components mode in the current context.
is_components_modebool—Returns whether the current context is in components mode. Returns: True if in components mode, False otherwise.
mark_update_called——Marks that .update() was explicitly called during the current handler.
was_update_calledbool—Returns whether .update() was explicitly called during the current handler. Returns: True if .update() was called, False otherwise.
reset_update_called——Resets the update-called flag for the current context.
auto_update_enabledbool—Returns whether auto-update is enabled in the current context. Returns: True if auto-update is enabled, False otherwise.
reset_auto_update——Starts a fresh auto-update state for the current context. The new state inherits the enclosing context's auto-update setting, or the app-wide default when the context has none of its own - which is the case for a session starting up, or for an event handler, since each event is dispatched in its own task. The _update_called flag is deliberately not inherited: it records whether .update() was called during this handler.

关注我,学习大模型、大数据、Python等热门技术。