Search code examples
pythonwindowsuser-interfacewindowfiremonkey

How to make a Python FMX GUI Form jump to the front?


I have a window Form that is created with the DelphiFMX GUI library for Python.

I want to know if there is a specific method that I can call to send the Form to the top above all other forms and/or windows from other apps.

Here's my current code. I want to make sure the NewForm is sent to the top when the button is clicked via the Button_OnClick method:

from delphifmx import *

class HelloForm(Form):
    def __init__(self, owner):
        self.Caption = 'Hello World'
        self.Width = 1000
        self.Height = 500
        self.Position = "ScreenCenter"

        self.myButton = Button(self)
        self.myButton.Parent = self
        self.myButton.Text = "Bring other form to front"
        self.myButton.Align = "Client"
        self.myButton.Margins.Top = 20
        self.myButton.Margins.Right = 20
        self.myButton.Margins.Bottom = 20
        self.myButton.Margins.Left = 20
        self.myButton.StyledSettings = ""
        self.myButton.TextSettings.Font.Size = 50
        self.myButton.onClick = self.Button_OnClick

        self.NewForm = Form(self)
        self.NewForm.Width = 1000
        self.NewForm.Height = 500
        self.NewForm.Show()

    def Button_OnClick(self, sender):
        # What code do I write here to send to front?

def main():
    Application.Initialize()
    Application.Title = "Hello World"
    Application.MainForm = HelloForm(Application)
    Application.MainForm.Show()
    Application.Run()
    Application.MainForm.Destroy()

main()

Solution

  • The easiest way to do this is to use the built-in BringToFront() method on every Form. You can add the following code to your Button_OnClick function:

    self.BringToFront()
    

    So your whole function would look like this:

    def Button_OnClick(self, sender):
        self.BringToFront() # Bring the form to front
    

    Just for some added information. If you want a Form to be permanently on top of every other window or app, then you can set the FormStyle property when you create the form:

    self.NewForm.FormStyle = "StayOnTop"