如何使Python FMX GUI窗体跳到最前面?

dm7nw8vv  于 2023-02-28  发布在  Python
关注(0)|答案(1)|浏览(163)

我有一个用DelphiFMX GUI library for Python创建的窗口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()
wfsdck30

wfsdck301#

最简单的方法是在每个Form上使用内置的BringToFront()方法。可以将以下代码添加到Button_OnClick函数中:

self.BringToFront()

所以你的整个函数应该是这样的:

def Button_OnClick(self, sender):
    self.BringToFront() # Bring the form to front

如果你想让一个表单永远位于其他窗口或应用程序的顶部,那么你可以在创建表单时设置FormStyle属性:

self.NewForm.FormStyle = "StayOnTop"

相关问题