Dearpygui的MessageBox

发布于 2025-01-24 10:42:31 字数 1289 浏览 0 评论 0原文

我正在使用DearpyGui库在Python中创建用户界面。

在运行我的算法的函数中,该算法需要用户的一些输入(是/否问题)。是否可以在不制动功能流程的情况下轻松地在DearpyGui中实现此类消息框?

类似的事情:

def runAlgorithm():
    a = 2 + 2
    user_choice = dpg.messageBox("Do you want to add or subtract the values", ["add", "subtract"]
    if user_choice == "add":
        a = a + a
    else:
        a = a - a
    print("The user has selected %s and the result is %d"%(user_choice, a)

正如我了解到到目前为止,这是不可能的,我必须编写类似的内容,这是相当笨重和不可读的,尤其是在可能需要某些用户输入的算法中,会大大破坏其可读性。

def choiceAdd(sender, app_data, user_data):
    a = user_data[0]
    print("The user has selected %s and the result is %d"%("add", a + a)
def choiceSub(sender, app_data, user_data):
    a = user_data[0]
    print("The user has selected %s and the result is %d"%("subtract", a - a)

def runAlgorithm():
    a = 2 + 2
    with dpg.window():
        dpg.add_button("add", callback = choiceAdd, user_data = [a])
        dpg.add_button("subtract", callback = choiceSub, user_data = [a]
    user_choice = dpg.messageBox("Do you want to add or subtract the values", ["add", "subtract"]
    if user_choice == "add":
        a = a + a
    else:
        a = a - a
    print("The user has selected %s and the result is %d"%(user_choice, a)

建议任何帮助。 先感谢您

I am using the DearPyGui library for creating a user interface in python.

In a function that runs my algorithm, the algorithm needs some input from the user (yes/no question). Is it possible to easily implement such a messagebox in DearPyGui without braking the flow of the function?

Something like this:

def runAlgorithm():
    a = 2 + 2
    user_choice = dpg.messageBox("Do you want to add or subtract the values", ["add", "subtract"]
    if user_choice == "add":
        a = a + a
    else:
        a = a - a
    print("The user has selected %s and the result is %d"%(user_choice, a)

As I understand DearPyGui until now, this is not possible and I have to write something like this, which is quite bulky and unreadable and especially in algorithms that might need some user input greatly disrupts their readability.

def choiceAdd(sender, app_data, user_data):
    a = user_data[0]
    print("The user has selected %s and the result is %d"%("add", a + a)
def choiceSub(sender, app_data, user_data):
    a = user_data[0]
    print("The user has selected %s and the result is %d"%("subtract", a - a)

def runAlgorithm():
    a = 2 + 2
    with dpg.window():
        dpg.add_button("add", callback = choiceAdd, user_data = [a])
        dpg.add_button("subtract", callback = choiceSub, user_data = [a]
    user_choice = dpg.messageBox("Do you want to add or subtract the values", ["add", "subtract"]
    if user_choice == "add":
        a = a + a
    else:
        a = a - a
    print("The user has selected %s and the result is %d"%(user_choice, a)

Any help is recommended.
Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2025-01-31 10:42:31

下面的代码可能会帮助

pythonic pythonic popup messagebox in dearppygui(asyncio或sync or sync or sync or sync or sync或sync )

目标:DearpyGui

do_sth_step1(...)
user_response = msgbox('Confirm?')  # Yes/No
if user_response == 'Yes':
    do_sth_step2(...)

解决方案中的Pythonic Popup Message Box:

import time
import inspect
import dearpygui.dearpygui as dpg
dpg.create_context()

dpg_callback_queue = []


def msgbox():

    def on_msgbox_btn_click(sender, data, user_data):
        nonlocal resp
        resp = dpg.get_item_label(sender)
        dpg.hide_item(popup)
        # dpg.delete_item(popup)

    with dpg.popup(parent=dpg.last_item(), modal=True) as popup:
        dpg.add_text('goto step 2?')
        with dpg.group(horizontal=True):
            dpg.add_button(label='Yes', callback=on_msgbox_btn_click)
            dpg.add_button(label='No', callback=on_msgbox_btn_click)
    dpg.show_item(popup)

    resp = None
    while not resp:
        # print('waiting for response ...')
        time.sleep(0.01)  # sleep main thread also
        handle_callbacks_and_render_one_frame()  # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    return resp


def on_btn_click(sender, data, user_data):
    print('do sth step 1 ...')
    resp = msgbox()
    if resp == 'Yes':
        print('do sth step 2 ...')
    print('done!')


def main():
    with dpg.window(tag='prim'):
        dpg.add_button(label='Submit', callback=on_btn_click)
    dpg.create_viewport(title='test msgbox', width=600, height=300)

    # start_dpg()
    dpg.setup_dearpygui()
    dpg.set_primary_window('prim', True)
    dpg.show_viewport()
    dpg.configure_app(manual_callback_management=True)
    while dpg.is_dearpygui_running():
        handle_callbacks_and_render_one_frame()
    dpg.destroy_context()


def run_callbacks():
    global dpg_callback_queue
    while dpg_callback_queue:
        job = dpg_callback_queue.pop(0)
        if job[0] is None:
            continue
        sig = inspect.signature(job[0])
        args = []
        for i in range(len(sig.parameters)):
            args.append(job[i+1])
        result = job[0](*args)


def handle_callbacks_and_render_one_frame():
    global dpg_callback_queue
    dpg_callback_queue += dpg.get_callback_queue() or []  # retrieves and clears queue
    # print(f'jobs: {jobs}')
    run_callbacks()
    dpg.render_dearpygui_frame()


if __name__ == '__main__':
    main()

和Asyncio方式:

import asyncio
import inspect
import dearpygui.dearpygui as dpg
dpg.create_context()


async def msgbox():

    def on_msgbox_btn_click(sender, data, user_data):
        # print('clicked msgbox button')
        nonlocal resp
        resp = dpg.get_item_label(sender)
        dpg.hide_item(popup)
        # dpg.delete_item(popup)

    with dpg.popup(parent=dpg.last_item(), modal=True) as popup:
        dpg.add_text('goto step 2?')
        with dpg.group(horizontal=True):
            dpg.add_button(label='Yes', callback=on_msgbox_btn_click)
            dpg.add_button(label='No', callback=on_msgbox_btn_click)
    dpg.show_item(popup)

    resp = None
    while not resp:
        # print('waiting for response ...')
        await asyncio.sleep(0.01)

    return resp


async def on_btn_click(sender, data, user_data):
    print('do sth step 1 ...')
    resp = await msgbox()
    if resp == 'Yes':
        print('do sth step 2 ...')
    print('done')


async def main():
    with dpg.window(tag='prim'):
        dpg.add_button(label='Submit', callback=on_btn_click)
    dpg.create_viewport(title='test msgbox', width=600, height=300)

    # start_dpg()
    dpg.setup_dearpygui()
    dpg.set_primary_window('prim', True)
    dpg.show_viewport()
    dpg.configure_app(manual_callback_management=True)
    while dpg.is_dearpygui_running():
        jobs = dpg.get_callback_queue()  # retrieves and clears queue
        await run_callbacks(jobs)
        dpg.render_dearpygui_frame()
        await asyncio.sleep(0.01)
    dpg.destroy_context()


async def run_callbacks(jobs):
    if jobs is None:
        pass
    else:
        for job in jobs:
            if job[0] is None:
                continue
            sig = inspect.signature(job[0])
            args = []
            for i in range(len(sig.parameters)):
                args.append(job[i+1])
            result = job[0](*args)
            if inspect.isawaitable(result):
                asyncio.create_task(result)


if __name__ == '__main__':
    asyncio.run(main())

codes below maybe help

pythonic popup messagebox in dearpygui (asyncio or sync way)

target: pythonic popup messagebox in dearpygui

do_sth_step1(...)
user_response = msgbox('Confirm?')  # Yes/No
if user_response == 'Yes':
    do_sth_step2(...)

solution:

import time
import inspect
import dearpygui.dearpygui as dpg
dpg.create_context()

dpg_callback_queue = []


def msgbox():

    def on_msgbox_btn_click(sender, data, user_data):
        nonlocal resp
        resp = dpg.get_item_label(sender)
        dpg.hide_item(popup)
        # dpg.delete_item(popup)

    with dpg.popup(parent=dpg.last_item(), modal=True) as popup:
        dpg.add_text('goto step 2?')
        with dpg.group(horizontal=True):
            dpg.add_button(label='Yes', callback=on_msgbox_btn_click)
            dpg.add_button(label='No', callback=on_msgbox_btn_click)
    dpg.show_item(popup)

    resp = None
    while not resp:
        # print('waiting for response ...')
        time.sleep(0.01)  # sleep main thread also
        handle_callbacks_and_render_one_frame()  # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    return resp


def on_btn_click(sender, data, user_data):
    print('do sth step 1 ...')
    resp = msgbox()
    if resp == 'Yes':
        print('do sth step 2 ...')
    print('done!')


def main():
    with dpg.window(tag='prim'):
        dpg.add_button(label='Submit', callback=on_btn_click)
    dpg.create_viewport(title='test msgbox', width=600, height=300)

    # start_dpg()
    dpg.setup_dearpygui()
    dpg.set_primary_window('prim', True)
    dpg.show_viewport()
    dpg.configure_app(manual_callback_management=True)
    while dpg.is_dearpygui_running():
        handle_callbacks_and_render_one_frame()
    dpg.destroy_context()


def run_callbacks():
    global dpg_callback_queue
    while dpg_callback_queue:
        job = dpg_callback_queue.pop(0)
        if job[0] is None:
            continue
        sig = inspect.signature(job[0])
        args = []
        for i in range(len(sig.parameters)):
            args.append(job[i+1])
        result = job[0](*args)


def handle_callbacks_and_render_one_frame():
    global dpg_callback_queue
    dpg_callback_queue += dpg.get_callback_queue() or []  # retrieves and clears queue
    # print(f'jobs: {jobs}')
    run_callbacks()
    dpg.render_dearpygui_frame()


if __name__ == '__main__':
    main()

and asyncio way:

import asyncio
import inspect
import dearpygui.dearpygui as dpg
dpg.create_context()


async def msgbox():

    def on_msgbox_btn_click(sender, data, user_data):
        # print('clicked msgbox button')
        nonlocal resp
        resp = dpg.get_item_label(sender)
        dpg.hide_item(popup)
        # dpg.delete_item(popup)

    with dpg.popup(parent=dpg.last_item(), modal=True) as popup:
        dpg.add_text('goto step 2?')
        with dpg.group(horizontal=True):
            dpg.add_button(label='Yes', callback=on_msgbox_btn_click)
            dpg.add_button(label='No', callback=on_msgbox_btn_click)
    dpg.show_item(popup)

    resp = None
    while not resp:
        # print('waiting for response ...')
        await asyncio.sleep(0.01)

    return resp


async def on_btn_click(sender, data, user_data):
    print('do sth step 1 ...')
    resp = await msgbox()
    if resp == 'Yes':
        print('do sth step 2 ...')
    print('done')


async def main():
    with dpg.window(tag='prim'):
        dpg.add_button(label='Submit', callback=on_btn_click)
    dpg.create_viewport(title='test msgbox', width=600, height=300)

    # start_dpg()
    dpg.setup_dearpygui()
    dpg.set_primary_window('prim', True)
    dpg.show_viewport()
    dpg.configure_app(manual_callback_management=True)
    while dpg.is_dearpygui_running():
        jobs = dpg.get_callback_queue()  # retrieves and clears queue
        await run_callbacks(jobs)
        dpg.render_dearpygui_frame()
        await asyncio.sleep(0.01)
    dpg.destroy_context()


async def run_callbacks(jobs):
    if jobs is None:
        pass
    else:
        for job in jobs:
            if job[0] is None:
                continue
            sig = inspect.signature(job[0])
            args = []
            for i in range(len(sig.parameters)):
                args.append(job[i+1])
            result = job[0](*args)
            if inspect.isawaitable(result):
                asyncio.create_task(result)


if __name__ == '__main__':
    asyncio.run(main())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文