window module¶
window¶
Module with several handy Python window classes for the toga framework
Copyright (c) 2020 Tom Arn, www.tanapro.ch
For suggestions and questions: <sw@tanapro.ch>
This file is distributed under the terms of the MIT license
- tatogalib.ui.window.centerOnParent(parent_window, child_window)¶
- window.version = '0.9.6'¶
- window.version_date = '2020-08-10 - 2023-11-07'¶
TaGui class¶
- class tatogalib.ui.window.TaGui(app, parentGui, title, **kwargs)¶
Template class for new GUIs. In the constructor, you can use all keyword arguments from Window or MainWindow Derived classes must call super().__init__() and implement the method build_gui() build_gui() must return a Widget (typically a Box or a container) which contains all content of the GUI. Then, the window can be shown by calling the show() method of the class
Example code (works the same for the main GUI and sub GUIs):
# in app.py: mygui = user_gui.MainGui( self, None, "Main GUI", size=(600, 480) # using None for the parentGui marks the main gui ) mygui.show() # in user_gui: class MainGui(TaGui): def __init__(self, app, parentGui, title, **kwargs): super().__init__(app, parentGui, title, **kwargs) # __init__ def build_gui(self) -> toga.Widget: # create box for content top_box = toga.Box(style=Pack(direction=COLUMN, flex=1)) top_box.add(toga.Label("Hello")) # button bar button_box = toga.Box(style=Pack(direction=ROW, padding=(5, 0, 0, 0))) # top, right, bottom and left padding button_box.add(toga.Label("", style=Pack(flex=1))) button_box.add(toga.Button("OK", on_press=self.handle_OK_button)) button_box.add(toga.Label("", style=Pack(flex=1))) top_box.add(button_box) return top_box # build_gui
Currently supported platforms: windows, android
Creates a new GUI class
- Parameters:
app (toga.App) – The app object
parentGui (TaGui) – The parent GUI of this GUI - must inherit from TaGui - Use None for the main window
title (str) – The title of the window to be created
kwargs – All keyword arguments allowed in MainWindow or TaWindow
- build_gui() Widget ¶
This method must be implemented by the derived class to create the user GUI
- Returns:
The root Widget which contains all of the user GUI elements
- Return type:
Typically a Box or some container
- close()¶
Closes the current GUI. On Android, it calls parentGui.show() to restore the previous GUI
- get_scale()¶
Returns the scale factor of the platform. Multiply dp values with this factor to get px values.
- Returns:
The scale factor
- Return type:
float
- get_window_size()¶
Returns the usable (width, height) of this window in dp
- Returns:
The size of the current window
- Return type:
(int, int)
- show()¶
Calls build_gui() and displays the GUI. Override build_gui() method in derived classes to implement the user GUI
TaWindow class¶
- class tatogalib.ui.window.TaWindow(parentWindow, title, size=(200, 200), position=None, auto_close_duration=None, on_close=None)¶
Extension of toga.Window with following features: - auto closeable - auto centered on parent window
This class is only supported on windows.
Creates a new TaWindow.
- Parameters:
parentWindow (toga.Window) – The toga.Window which is the parent of this TaWindow
title (str) – The title for this window
size (tuple[int, int]) – The initial size (width, height) in dp of this HtmlWindow
position (tuple[int, int] or None) – The initial position (x, y) of this HtmlWindow. None centers it on parentWindow
auto_close_duration (float or None) – The time in seconds after which this HtmlWindow closes automatically
on_close – The callable that will be called when the user closes the window
- close()¶
Cancels a possibly active auto-close timer and closes the window
- show()¶
Shows the window
- window_close_handler(window)¶
Cancels a possibly active auto-close timer when the window should close
- Returns:
True when the window should close, False when it should stay open
- Return type:
bool
HtmlWindow class¶
- class tatogalib.ui.window.HtmlWindow(parentWindow, title, html_text, size=(200, 200), font_size=None, position=None, auto_close_duration=None, on_close=None)¶
Class which shows a TaWindow with html content. If no position is passed, the window will center on its parent
This class is only supported on windows.
Creates a window with a WebView.
- Parameters:
parentWindow (toga.Window) – The toga.Window which is the parent of this HtmlWindow
title (str) – The title for this window
size (tuple[(int, int)]) – The initial size (width, height) in dip of this HtmlWindow
html_text (str) – The html text to display
position (tuple[(int, int)] or None) – The initial position (x, y) of this HtmlWindow. None centers it on parentWindow
auto_close_duration (float or None) – The time in seconds after which this HtmlWindow closes automatically
on_close – The callable that will be called when the user closes the window
- add_ok_button()¶
Adds an OK button at the bottom.
- show()¶
Shows the window