Skip to content

armgit / BUI

Public Imported from GitHub
0

Blender's GPU Accelerated UI, Decoupled from it's original codebase. Supports Rust, Python, JS, React, C & C++. Twice as good as GPUI

fix(python): keep hidden tk root out of tkinter default root

Krish Kalaria8b58b807835e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d9516d9..ea1b0ea 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -201,6 +201,7 @@ The newest work. Details for some items follow in the sections below.
### Fixed
- **GPU text:** `BUI_gpu_slug()` read the environment on every text run, which pushed the GPU raster from 1.6 to 9.4 ms per frame at 3,000 cells. It now reads it once.
- **`WindowHandle::update`:** `cx.notify()` inside the closure didn't re-render that window.
+- **Python, hidden Tk root:** BUI's hidden Tk root (started before the first window on macOS, and by the Tk host) became `tkinter._default_root`, so a real tkinter program's `StringVar()` bound to the hidden interpreter and its widgets never saw the value.
### Changed
- **Text looks like Blender's:** Inter, Blender's UI font, is embedded and is the default face on every platform. The portable stb_truetype engine is now the default on Windows too. `-DBUI_TEXT_GDI=ON` (Rust: the `gdi-text` feature) restores GDI and system fonts. `BUI_TEXT_STB` and `portable-text` are now no-ops.
diff --git a/bindings/python/bui/host.py b/bindings/python/bui/host.py
index 8af5fe5..7a06a2e 100644
--- a/bindings/python/bui/host.py
+++ b/bindings/python/bui/host.py
@@ -78,7 +78,11 @@ def _tk():
global _tk_root
tk = _realtk.module()
if _tk_root is None:
+ # keep it out of tkinter's default root: a program's own tk.StringVar() and
+ # friends must not bind to this hidden interpreter
+ default_root = tk._default_root
_tk_root = tk.Tk()
+ tk._default_root = default_root
_tk_root.withdraw()
return _tk_root
diff --git a/bindings/python/tests/test_tk_compat.py b/bindings/python/tests/test_tk_compat.py
index 4db100f..d121795 100644
--- a/bindings/python/tests/test_tk_compat.py
+++ b/bindings/python/tests/test_tk_compat.py
@@ -63,9 +63,6 @@ def prog_calculator(tk, ttk):
buttons["C"].invoke()
entry.insert(0, "6*7")
buttons["="].invoke()
- # a textvariable write reaches the Entry at idle time in Tk on macOS: flush it
- # before reading the widget (without this, stdlib Tk there still shows "6*7")
- root.update_idletasks()
out.append(entry.get())
info = buttons["5"].grid_info()
result = {"results": out, "grid": (info["row"], info["column"]), "size": root.grid_size(),
@@ -462,7 +459,11 @@ def _real_modules():
tk = _realtk.module("tkinter")
import importlib
ttk = importlib.import_module("tkinter.ttk")
- tk.Tk().destroy()
+ # Tk 9 on macOS: the next Tk() crashes if the only root was destroyed before
+ # its idle events ran
+ root = tk.Tk()
+ root.update_idletasks()
+ root.destroy()
return tk, ttk
except Exception:
return None
@@ -537,7 +538,27 @@ def test_install_runs_unchanged_scripts():
sys.modules.update(saved)
+def test_hidden_root_is_not_default_root():
+ """BUI's hidden Tk root (macOS, the Tk host) must not capture a real tkinter
+ program's variables: they belong to the program's own Tk()."""
+ real = _real_modules()
+ if real is None:
+ print("skipped hidden root test (no stdlib Tk)")
+ return
+ tk = real[0]
+ from bui import host
+ host._tk()
+ root = tk.Tk()
+ var = tk.StringVar()
+ entry = tk.Entry(root, textvariable=var)
+ var.set("linked")
+ seen = entry.get()
+ root.destroy()
+ assert seen == "linked", "StringVar went to the hidden root: Entry shows %r" % seen
+
+
def main() -> int:
+ test_hidden_root_is_not_default_root()
test_programs_match_stdlib()
test_install_runs_unchanged_scripts()
print("tkinter compatibility tests passed")