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

如何在Python DelphiFMX GUI应用中显示MessageBox/ShowMessage弹窗?

在DelphiFMX Python应用中实现弹窗对话框

你可以通过以下两种方式实现按钮点击后的弹窗,直接修改myButtonClick方法即可:

方法1:使用ShowMessage(简单弹窗)

这是最快捷的方式,直接调用内置的ShowMessage函数,传入要显示的文本:

修改后的myButtonClick方法:

def myButtonClick(self, sender):
    ShowMessage("按钮已点击!这是ShowMessage弹窗")

方法2:使用MessageBox(自定义弹窗)

如果需要自定义弹窗标题、按钮组合或图标,可以使用MessageBox函数,它需要传入父窗口句柄、弹窗文本、标题以及样式参数:

修改后的myButtonClick方法:

def myButtonClick(self, sender):
    # MB_OK 表示仅显示确定按钮,MB_ICONINFORMATION 表示显示信息图标
    MessageBox(self.Handle, "这是MessageBox弹窗", "自定义弹窗标题", MB_OK | MB_ICONINFORMATION)

完整示例代码

替换原代码中的myButtonClick方法后,完整代码如下:

from delphifmx import *

class frmMain(Form):
    def __init__(self, owner):
        self.Caption = 'My Form'
        self.Width = 800
        self.Height = 500

        self.myButton = Button(self)
        self.myButton.Parent = self
        self.myButton.Position.X = 100
        self.myButton.Position.Y = 100
        self.myButton.Width = 200
        self.myButton.Height = 200
        self.myButton.Text = "Click me"

        self.myButton.OnClick = self.myButtonClick

    def myButtonClick(self, sender):
        # 二选一:使用ShowMessage
        ShowMessage("按钮已点击!这是ShowMessage弹窗")
        
        # 或者使用MessageBox(注释掉上面一行,取消下面注释)
        # MessageBox(self.Handle, "这是MessageBox弹窗", "自定义弹窗标题", MB_OK | MB_ICONINFORMATION)


def main():
    Application.Initialize()
    Application.Title = "My Application"
    Application.MainForm = frmMain(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.29 16:58:38