You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python FMX GUI应用中设置Form的最小/最大尺寸?

DelphiFMX for Python:设置窗体最小/最大尺寸的最优方案

你当前通过OnResize事件修正窗体尺寸的方法确实能生效,但存在体验上的瑕疵(拖拽时会先出现尺寸超限再跳回的情况)。而你尝试的MinWidth/MaxWidth等属性无效,是因为在DelphiFMX框架中,这些尺寸约束需要通过窗体的Constraints对象来配置,而非直接给Form实例赋值。

正确的实现方式

直接操作Form.Constraints的对应属性,系统会在底层限制窗体的可调整范围,无需依赖OnResize事件:

from delphifmx import *

class frmMain(Form):
    def __init__(self, owner):
        self.Caption = 'My Form'
        self.Width = 400
        self.Height = 200
        # 通过Constraints设置尺寸约束
        self.Constraints.MinWidth = 400
        self.Constraints.MaxWidth = 1000
        self.Constraints.MinHeight = 200
        self.Constraints.MaxHeight = 800

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

main()

为什么原来的方法有问题?

  1. 体验不佳:OnResize事件是在尺寸变化后触发,用户拖拽时会先看到窗体超出限制尺寸,再被强制拉回,视觉上有跳变感。
  2. 冗余代码:需要手动编写判断逻辑,不如原生约束简洁可靠。

关于Constraints对象

Constraints是DelphiFMX中专门用于控制控件(包括Form)尺寸范围的内置对象,除了最小/最大宽高,还支持设置MinSize、MaxSize等组合属性,能更灵活地管理尺寸限制。

内容的提问来源于stack exchange,提问作者Shaun Roselt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 17:13:14