Additional Windows
While your app is the main stage, sometimes you need a side-room for things like Settings, Help, or Edit screens. These are called sub-windows.
Creating a Window
Section titled “Creating a Window”You create a new window with the gp.Window() constructor, which takes a title as an argument:
my_window = gp.Window(title)This creates a new window with the specified title. You can then add widgets to this window using my_window.add(), just like you do with the main window.
Familiar features
Section titled “Familiar features”The great news is that a sub-window acts almost exactly like your main window. You can use all these properties you already know:
- Settings:
.resizable_horizontal,.resizable_vertical,.set_icon() - Sizing:
.width,.height,set_size(w, h)
Showing and Hiding Windows
Section titled “Showing and Hiding Windows”Here is the big difference: Sub-windows always start hidden. They wait in the background until you tell them to appear.
| Method | What happens? |
|---|---|
.show() | Makes the window visible to the user. |
.hide() | Tucks the window away. It’s still “alive”, just invisible. |
.show_on_top() | Shows the window and forces it to the front of all other windows. |
Note: If a user clicks the “X” on a sub-window, GooeyPie doesn’t destroy the window or quit the whole app - it just calls .hide() for you!
Example: Multiple Windows
Section titled “Example: Multiple Windows”In this example, we have two buttons on the main window that open two different sub-windows. The “Open on top” button opens a window that will always stay in front of the main window, while the “Open other window” button opens a regular sub-window that can be hidden behind the main window.
import gooeypie as gp
def open_on_top_window(event): on_top_window.show_on_top()
def open_other_window(event): other_window.show()
# Create main windowapp = gp.GooeyPieApp('Other windows')
on_top_btn = gp.Button('Open on top', open_on_top_window)open_other_btn = gp.Button('Open other window', open_other_window)app.add(on_top_btn, 1, 1)app.add(open_other_btn, 1, 2)
# Create on top windowon_top_window = gp.Window('On top window')on_top_message = gp.Label('This window is on top')on_top_window.add(on_top_message, 1, 1)
# Create other windowother_window = gp.Window('Other window')other_message = gp.Label('This is another window')other_window.add(other_message, 1, 1)
app.run()Sub-window Events
Section titled “Sub-window Events”Because sub-windows are are only ever hidden and not destroyed, re-using a sub-window can lead to old data still being there when you show it again. To fix this, sub-windows have two special event listeners:
my_window.on_show(my_function)Runs a function right as the window is appearing. Use this to clear out old data or reset sliders so the window looks brand new every time it opens.
my_window.on_hide(my_function)Runs a function right as the window is closed/hidden. This is perfect for auto-saving changes.
Summary: App vs Windows
Section titled “Summary: App vs Windows”| Feature | Main Window | Sub-windows |
|---|---|---|
| How to create | app = gp.GooeyPieApp('My App') | my_window = gp.Window('My Window') |
| Starts visible? | Yes, when run() is called. | No, starts hidden. Must call show(). |
| Closing with “X” button | Quits the entire program. | Just hides that window. |
| Special Events | on_load(), on_close() | on_show(), on_hide() |