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

如何让Python FMX GUI的Form窗口前置显示?

DelphiFMX for Python:将Form置于所有窗口顶部的方法

我使用DelphiFMX for Python GUI库创建了名为Form的窗口,想了解是否有特定方法可以让该Form置于所有其他Form以及第三方应用窗口的顶部。以下是我的代码,希望点击按钮时,通过Button_OnClick方法让NewForm前置显示:

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()

解决方案

有两种常用方式可以实现需求:

1. 临时将Form前置(仅点击时置顶,后续可被其他窗口覆盖)

直接调用Form的BringToFront()方法,这是FMX框架的标准方法,会将目标窗口带到所有窗口的最前端:

def Button_OnClick(self, sender):
    self.NewForm.BringToFront()

2. 让Form始终保持置顶(持续在所有窗口上方)

如果需要NewForm一直保持在其他窗口顶部,可修改它的FormStyle属性为"StayOnTop",可以在初始化或点击事件中设置:

# 初始化时设置始终置顶
self.NewForm = Form(self)
self.NewForm.Width = 1000
self.NewForm.Height = 500
self.NewForm.FormStyle = "StayOnTop"
self.NewForm.Show()

# 或者在点击事件中切换置顶状态
def Button_OnClick(self, sender):
    if self.NewForm.FormStyle == "Normal":
        self.NewForm.FormStyle = "StayOnTop"
    else:
        self.NewForm.FormStyle = "Normal"

完整修改后的代码示例

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):
        # 临时前置
        self.NewForm.BringToFront()
        # 如果要切换始终置顶状态,替换上面一行为下面的代码:
        # self.NewForm.FormStyle = "StayOnTop" if self.NewForm.FormStyle == "Normal" else "Normal"

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

main()

内容的提问来源于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.30 10:00:22