Repository navigation
How to make Window.show_dialog block the execution of the main window and application? #7084
Replies: 1 comment
|
There are two patterns that work (Open3D 0.18–0.20): 1. Continuation style (simplest). Don't wait. Run the "after" code from the Accept handler: def ask_parameters(self, on_done):
dlg = gui.Dialog("Parameter selection")
... # build layout + gui_elements as you do now
def _on_accept():
window.close_dialog()
on_done(read_values(gui_elements)) # read your widgets here (e.g. .double_value, .text_value)
accept.set_on_clicked(_on_accept)
window.show_dialog(dlg)
# usage
self.ask_parameters(lambda params: self.run_algorithm(params))2. Blocking style, from a worker thread. If the code that needs the parameters is long-running anyway, run it with import threading
def worker():
done = threading.Event()
result = {}
def show(): # runs on the main thread
dlg = build_dialog(result, done) # in _on_accept: window.close_dialog(); fill result; done.set()
window.show_dialog(dlg)
gui.Application.instance.post_to_main_thread(window, show)
done.wait() # blocks only this worker thread
heavy_processing(result)
gui.Application.instance.post_to_main_thread(window, update_ui_with_results)
gui.Application.instance.run_in_thread(worker)Rule: inside To check: with either pattern the dialog appears and stays responsive. The code after it runs only once you press Accept. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I have several elements with callbacks that I add to a dialog:
I then want to call
window.show_dialog(dlg)so it waits until I get all the parameters from the user before doing something.Currently,
show_dialogdoes not block the rest of the application, which I think is by design. However, in my case this is necessary, otherwise the function would run before getting the parameters.I tried solving this problem with this:
But the result is that now everything is blocked and I don't even see the dialog.
What is the correct way of doing this?
All reactions