Skip to content

Commit

Permalink
[gui] [ipython] Add GUI.__del__ to close on variable dereference (#1869)
Browse files Browse the repository at this point in the history
* better pybuf_enabled detection

* add GUI.__del__ to close on variable dereference

* fix win32 not destroying window

* [skip ci] Update python/taichi/lang/shell.py

* nit

* [skip ci] Update python/taichi/misc/gui.py
  • Loading branch information
archibate committed Sep 26, 2020
1 parent 1446e8a commit 3b62912
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion python/taichi/lang/shell.py
Expand Up @@ -15,7 +15,8 @@
_env_enable_pybuf = os.environ.get('TI_ENABLE_PYBUF', '1')
if not _env_enable_pybuf or int(_env_enable_pybuf):
# When using in Jupyter / IDLE, the sys.stdout will be their wrapped ones.
pybuf_enabled = type(sys.stdout).__name__ != 'TextIOWrapper'
# While sys.__stdout__ should always be the raw console stdout.
pybuf_enabled = sys.stdout is not sys.__stdout__

from .core import taichi_lang_core
taichi_lang_core.toggle_python_print_buffer(pybuf_enabled)
Expand Down
6 changes: 5 additions & 1 deletion python/taichi/misc/gui.py
Expand Up @@ -59,6 +59,9 @@ def __enter__(self):
def __exit__(self, type, val, tb):
self.close()

def __del__(self):
self.close()

def close(self):
self.core = None # dereference to call GUI::~GUI()

Expand Down Expand Up @@ -342,7 +345,8 @@ def text(self, content, pos, font_size=15, color=0xFFFFFF):
color = ti.core_vec(r, g, b, 1)
self.canvas.text(content, pos, font_size, color)

def _make_field_base(gui, w, h, bound):
@staticmethod
def _make_field_base(w, h, bound):
x = np.linspace(bound / w, 1 - bound / w, w)
y = np.linspace(bound / h, 1 - bound / h, h)
base = np.array(np.meshgrid(x, y))
Expand Down
1 change: 1 addition & 0 deletions taichi/gui/win32.cpp
Expand Up @@ -223,6 +223,7 @@ GUI::~GUI() {
if (show_gui) {
std::free(data);
DeleteDC(src);
DestroyWindow(hwnd);
gui_from_hwnd.erase(hwnd);
}
}
Expand Down

0 comments on commit 3b62912

Please sign in to comment.