Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window freezes when using stop_dearpygui #2013

Closed
michael-salmas opened this issue Jan 11, 2023 · 8 comments
Closed

Window freezes when using stop_dearpygui #2013

michael-salmas opened this issue Jan 11, 2023 · 8 comments
Labels
state: pending not addressed yet type: bug bug

Comments

@michael-salmas
Copy link

michael-salmas commented Jan 11, 2023

Version of Dear PyGui

Version: 1.8.0
Operating System: Windows 11

My Issue/Question

When using the method dpg.stop_dearpygui() the application wont begin to close until all code is finished running in the script.

To Reproduce

Steps to reproduce the behavior:

  1. Run the code below
  2. click the EXIT button created in the widow
  3. observe the window freezes until the time.sleep(10) command is completed. (Specifically that the loading mouse icon doesnt even begin until the time.sleep(10) is finished.

Expected behavior

I would expect the window to close before executing the code, as this prevents me from using any alert boxes in the remaining parts of my script

Screenshots/Video

Can provide if needed

Standalone, minimal, complete and verifiable example

# Here's some code anyone can copy and paste to reproduce your issue
import dearpygui.dearpygui as dpg

def destroy():
    dpg.stop_dearpygui()

dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()

with dpg.window(label="Example Window"):
    dpg.add_button(label="EXIT", callback=destroy)

dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

import time

print("Starting sleeping")
time.sleep(10)
print("Done sleeping")
@michael-salmas michael-salmas added state: pending not addressed yet type: bug bug labels Jan 11, 2023
@michael-salmas
Copy link
Author

I found a work around for displaying the alert boxes I need by moving dpg.destroy_context() to the very end of my script. Is this the intended workaround?

@IvanNazaruk
Copy link

Try this workaround:
(In the example, we send a WM_CLOSE (close window) message to the window, and it closes and stops the DPG)

import ctypes
import os
from ctypes.wintypes import HWND, LPARAM, BOOL

import dearpygui.dearpygui as dpg

WM_CLOSE = 16
WNDENUMPROC = ctypes.WINFUNCTYPE(HWND,
                                 BOOL,
                                 LPARAM)


def get_hwnd_from_pid(pid: int) -> int | None:
    result = None

    def callback(hwnd, _):
        nonlocal result
        cpid = ctypes.c_ulong()
        ctypes.windll.user32.GetWindowThreadProcessId(hwnd, ctypes.byref(cpid))
        cpid = cpid.value

        if cpid == pid:
            result = hwnd
            return False
        return True

    cb_worker = WNDENUMPROC(callback)
    ctypes.windll.user32.EnumWindows(cb_worker, 0)
    return result


def destroy():
    hwnd = get_hwnd_from_pid(os.getpid())
    ctypes.windll.user32.SendMessageW(hwnd, WM_CLOSE, 0, 0)


dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()

with dpg.window(label="Example Window"):
    dpg.add_button(label="EXIT", callback=destroy)

dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

import time

print("Starting sleeping")
time.sleep(10)
print("Done sleeping")

@michael-salmas
Copy link
Author

That works great, thank you for the help!

@Ondrejtra
Copy link

Ondrejtra commented Jan 11, 2023

I'm not certain whether dpg.destroy_context() is a safe/inteded way to close the viewport, but it seems to do the trick.

def destroy():
    dpg.stop_dearpygui()
    dpg.destroy_context()

The cursor buffers, then the viewport closes in about half a second (this even happens when dpg.stop_dearpygui() is not there). Additionally, this solution is platform-independent.

@michael-salmas
Copy link
Author

michael-salmas commented Jan 11, 2023

Interesting, I seem to get slightly different results from my code when I play around with where those 2 commands are run...

but thanks for that info, I think with some clever placements I can make it work how I want

@v-ein
Copy link
Contributor

v-ein commented Aug 3, 2023

I think this is still a valid issue.

import time
import dearpygui.dearpygui as dpg

dpg.create_context()
dpg.create_viewport(width=600, height=600)
dpg.setup_dearpygui()

with dpg.window(label="Test") as wnd:
    dpg.add_button(label="Exit", callback=lambda: dpg.stop_dearpygui())

dpg.show_viewport()
dpg.start_dearpygui()
print("Exited from start_dearpygui")
time.sleep(3)
print("Before destroy_context")
dpg.destroy_context()
print("After destroy_context")
time.sleep(3)
print("Quit")

If you run this on Windows, clicking the Exit button does not close the window; neither does it close when destroy_context is invoked.

This is important: destroy_context does not close the window. The window only disappears when Pytnon process terminates, that is, due to cleanup done by the operating system.

Inside of DPG, there is a function named mvCleanupViewport that does destroy the window. However, this function is not called anywhere. I bet it was an implementation for cleanup_dearpygui, which is now deprecated, so the implemenation is just hanging in the air.

Shouldn't destroy_context call mvCleanupViewport under the cover? I'm not sure whether it was a design decision or an oversight.

@frederick0291
Copy link

Having this issue as well.

@v-ein
Copy link
Contributor

v-ein commented Jul 2, 2024

#2275 will fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: pending not addressed yet type: bug bug
Projects
None yet
Development

No branches or pull requests

6 participants