isl-org / isl-org/Open3D

The bad and the Evil: tkinter - My final conclusions up X - BadWindow errors

Open
#6,125 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
14k
Forks
2.6k
Avg merge
5d 18h
Merged PRs (30d)
6

Description

As i have lately found a thread on stackoverflow where combination of `Tkinter` and open3D issue i further persued my investigations. The result thereof is. `Tkinter` does not care about whether there is other autonomous gui window. It just paints in the active window assuming its own main window.

When any open3d `visualization` function or custom `visualization.gui` is running this will cause python be terminated.
I tried many things i do not list here. I finally resorted to use `subprocess` module to sandbox the two `Tkinter` dialogs i'm using.
This was the easiere as file/dir name and yes, no, cancel are text strings anyway.

```Python
import sys
import os
import subprocess

def askdirectory(title="pick dir",initialdir=None,mustexist=False):
if initialdir:
initialdir = os.getcwd()
try:
outcome = subprocess.run(["python3","-c","""
import sys
import tkinter as tk,tkinter.filedialog as filedialog

try:
root = tk.Tk()
root.withdraw()
new_save_path = filedialog.askdirectory(parent = root,title = "{}",initialdir = "{}",mustexist={})
except:
raise
if not new_save_path:
sys.exit(5)
print(new_save_path,end="")
sys.exit(0)
""".format(title,initialdir,mustexist)],capture_output=True,text=True,check=True,shell=False)
except subprocess.CalledProcessError as fail:
if fail.returncode == 5:
return ''
raise
return outcome.stdout

def askyesnocancel(title=None,message=None):
if title is None:
title = "What think about?"
if message is None:
message = title
try:
outcome = subprocess.run(["python3","-c","""
import sys
import tkinter as tk,tkinter.messagebox as messagebox

try:
root = tk.Tk()
root.withdraw()
outcome = messagebox.askyesnocancel(parent = root,title = \"\"\"{}\"\"\",message = \"\"\"{}\"\"\")
except:
raise
if outcome is None:
sys.exit(5)
print(outcome,end="")
sys.exit(0)
""".format(title,message)],capture_output=True,text=True,check=True,shell=False)
except subprocess.CalledProcessError as fail:
if fail.returncode == 5:
return None
raise
if outcome.stdout in ('Yes','yes','True','true','1','On','on'):
return True
if outcome.stdout in ('No','no','False','false','0','Off','off'):
return False
raise ValueError("dialog returnd unknonw value '{}'".format(outcome.stdout))
```

With this i still can use the tkinter dialogs without any negative interference with my `o3d.visualization.gui`

Related to or solution along #4427 #1715

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.