Search code examples
pythonuser-interfacefiremonkeycentering

How to specify where a Python FMX GUI Form opens?


I'm creating a Form using DelphiFMX GUI library for Python and I would like to choose a specific location where the Form should open. Here's the code that I use to create my Form:

from delphifmx import *

class frmMain(Form):
    def __init__(self, owner):
        self.Caption = 'My Form'
        self.Width = 1000
        self.Height = 500

def main():
    Application.Initialize()
    Application.Title = "My Application"
    Application.MainForm = frmMain(Application)
    Application.MainForm.Show()
    Application.Run()
    Application.MainForm.Destroy()

main()

Are there any built-in functions, procedures, or properties that I can use to change the position of the Form?

For some of my forms, I want them to be centered and for others, I want to set a custom X and Y coordinate.


Solution

  • There's a built-in Position property on every Form as well as Left and Top properties. You can add the following line of code to center your Form when it starts:

    self.Position = "ScreenCenter"
    

    Some of the other options you have in the Position property are:

    FireMonkey Form Position Property

    When you set the Position to Designed, then you can specify custom Left and Top values for it:

    self.Position = "Designed"
    self.Top = 420
    self.Left = 69
    

    Top is the Y coordinate and Left is the `X coordinate.