pythonuser-interfaceresizefiremonkeywindow-resize

How to set min/max dimensions of a Form in a Python FMX GUI App?


I've made a Form using the DelphiFMX GUI Library for Python and it's working perfectly fine, but I want to set a minimum and maximum size for the Form to make sure the window can't resize below or above that amount.

I have code that is working. I've written code that keeps the Width above 400 and below 1000, and keeps the Height below 800 and above 200. I'm using the OnResize event of the Form to check the Height and Width and then set it accordingly. Here's my full code for creating the Form and assigning the OnResize event to it:

from delphifmx import *

class frmMain(Form):
    def __init__(self, owner):
        self.Caption = 'My Form'
        self.Width = 400
        self.Height = 200
        self.OnResize = self.FormOnResizeEvent

    def FormOnResizeEvent(self, sender):
        if self.Width < 400:
            self.Width = 400
        elif self.Width > 1000:
            self.Width = 1000

        if self.Height < 200:
            self.Height = 200
        elif self.Height > 800:
            self.Height = 800


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

main()

But is there a better way to do this?

I tried doing:

self.MinWidth = 400
self.MaxWidth = 1000
self.MinHeight = 200
self.MaxHeight = 800

But this doesn't work at all.


Solution

  • The correct way to set a minimum/maximum size for a Form in FMX is using the Constraints property on the Form. The code below should do the trick:

    self.Constraints.MinWidth = 400
    self.Constraints.MaxWidth = 1000
    self.Constraints.MinHeight = 200
    self.Constraints.MaxHeight = 800