Platform¶
Usage¶
Although Toga is a cross-platform framework, and in theory shouldn't require any special platform-specific handling, in practice you will. If you wish to apply platform-specific modifications to your user interface, or modify business logic on a per-platform basis, you may need to know the currently active backend, or the platform on which your application is running. This can be achieved by using toga.backend and toga.platform.current_platform.
For example:
import toga
if toga.backend == `toga_gtk`:
# ... perform GTK-specific logic
if toga.platform.current_platform == 'android':
# ... perform Android-specific business logic
Selecting a specific backend¶
In general, a Python environment should only have a single Toga backend installed. However, if you need to install multiple backends, you can tell Toga which backend to use by setting the TOGA_BACKEND environment variable to match the name of the Python module for the backend you wish to use (e.g., toga_gtk).
Getting an implementation factory¶
Developers who want to implement new platform-dependent functionality, or produce a new backend, need a way to access the implementation classes for the current backend. The get_factory function provides a standard way to do this, returning an object whose attributes are lazily-loaded implementation classes.
See the topic guide on Extending Toga for more information.
Reference¶
The name of the backend that is being used by Toga to implement
platform-specific capabilities (e.g., toga_cocoa, toga_gtk).
A string identifier of the platform on which the application is currently running. One of:
androidmacOSiOSlinuxfreeBSDwebwindows
DEPRECATED: This property exists for historical reasons. On Python 3.13 and later,
you can use the Python standard library property [sys.platform][].
It is required on Python 3.12 and earlier because Android historically returned
sys.platform == "linux" until the android value was formalied by PEP 783. The names
used by current_platform do not exactly match the names returned by
[sys.platform][].
Return the implementation factory for an interface group.
The object that is returned is a namespace whose attributes are the implementation classes for the current backend contributed by the appropriate entry points.
:param interface: the name of the interface group for the factory, or None
for the default "toga_core" interface. Third-party interface group
names should start with "togax_".
:returns: The factory namespace object.
Source code in core/src/toga/platform.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |