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

如何在DearPyGui中实现Python风格的同步/异步弹窗消息框?

在DearPyGui中实现Python风格的确认弹窗

要实现你需要的Yes/No确认弹窗逻辑,DearPyGui自带的dpg.show_message_box()函数就能满足需求,它是阻塞式调用,会等待用户操作后返回选择结果。

基础实现示例

import dearpygui.dearpygui as dpg

def do_sth_step1():
    print("完成步骤1")

def do_sth_step2():
    print("完成步骤2")

dpg.create_context()
dpg.create_viewport(title='弹窗示例', width=600, height=400)
dpg.setup_dearpygui()

# 执行第一步操作
do_sth_step1()

# 弹出确认弹窗,按钮为Yes/No
# 返回值对应按钮索引:0=Yes,1=No
user_choice = dpg.show_message_box("确认", "Confirm?", modal=True, buttons=dpg.mvMessageBox_Button_YesNo)

# 根据用户选择执行后续逻辑
if user_choice == 0:
    do_sth_step2()

dpg.start_dearpygui()
dpg.destroy_context()

封装成你想要的msgbox函数

如果想直接返回字符串类型的"Yes"/"No",可以封装一个简单的工具函数,完全贴合你期望的代码逻辑:

import dearpygui.dearpygui as dpg

def msgbox(prompt):
    # 弹窗返回0代表点击Yes,1代表点击No
    choice = dpg.show_message_box("确认", prompt, modal=True, buttons=dpg.mvMessageBox_Button_YesNo)
    return "Yes" if choice == 0 else "No"

def do_sth_step1():
    print("完成步骤1")

def do_sth_step2():
    print("完成步骤2")

dpg.create_context()
dpg.create_viewport(title='封装弹窗示例', width=600, height=400)
dpg.setup_dearpygui()

# 完全符合你想要的调用逻辑
do_sth_step1()
user_response = msgbox('Confirm?')
if user_response == 'Yes':
    do_sth_step2()

dpg.start_dearpygui()
dpg.destroy_context()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 20:03:21