我使用
DelphiFMX GUI Library for Python制作了一个
Form
,它工作得很好,但我想为表单设置最小和最大尺寸,以确保窗口不能调整到低于或高于该尺寸金额。
我有可用的代码。我编写的代码使
Width
保持在400以上和1000以下,并将Height
保持在800以下和200以上。我正在使用OnResize
的Form
事件来检查Height
和Width
然后相应地设置它。这是我创建 Form
并将 OnResize
事件分配给它的完整代码:
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()
但是有更好的方法吗?
我试过做:
self.MinWidth = 400
self.MaxWidth = 1000
self.MinHeight = 200
self.MaxHeight = 800
但这根本行不通
在 FMX 中为
Form
设置最小/最大大小的正确方法是使用 Constraints
上的 Form
属性。下面的代码应该可以解决问题:
self.Constraints.MinWidth = 400
self.Constraints.MaxWidth = 1000
self.Constraints.MinHeight = 200
self.Constraints.MaxHeight = 800