From 479b37b59c0cc2331cbbe7225c2b6e7ccc4a1294 Mon Sep 17 00:00:00 2001 From: u214892 <u214892@sbb.ch> Date: Thu, 4 Jul 2019 11:44:07 +0200 Subject: [PATCH] switch for Tk.Toplevel --- benchmarks/benchmark_all_examples.py | 4 ++++ benchmarks/profile_all_examples.py | 4 +++- benchmarks/run_all_examples.py | 7 +++++++ flatland/utils/graphics_pil.py | 14 +++++++++++--- tox.ini | 3 ++- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/benchmarks/benchmark_all_examples.py b/benchmarks/benchmark_all_examples.py index 676bfe1..5687ad8 100644 --- a/benchmarks/benchmark_all_examples.py +++ b/benchmarks/benchmark_all_examples.py @@ -9,6 +9,10 @@ import pkg_resources from benchmarker import Benchmarker from importlib_resources import path +from flatland.utils import graphics_pil + +graphics_pil.unattended_switch = True + for entry in [entry for entry in importlib_resources.contents('examples') if not pkg_resources.resource_isdir('examples', entry) and entry.endswith(".py") diff --git a/benchmarks/profile_all_examples.py b/benchmarks/profile_all_examples.py index f0280f1..b41f52a 100644 --- a/benchmarks/profile_all_examples.py +++ b/benchmarks/profile_all_examples.py @@ -8,11 +8,11 @@ import pkg_resources from importlib_resources import path from benchmarks.benchmark_utils import swap_attr +from flatland.utils import graphics_pil def profile(resource, entry): with path(resource, entry) as file_in: - # TODO remove input() from examples print("*****************************************************************") print("Profiling {}".format(entry)) @@ -25,6 +25,8 @@ def profile(resource, entry): cProfile.run('my_func()', sort='time') +graphics_pil.unattended_switch = True + for entry in [entry for entry in importlib_resources.contents('examples') if not pkg_resources.resource_isdir('examples', entry) and entry.endswith(".py") diff --git a/benchmarks/run_all_examples.py b/benchmarks/run_all_examples.py index 8cc940a..972ac68 100644 --- a/benchmarks/run_all_examples.py +++ b/benchmarks/run_all_examples.py @@ -7,14 +7,21 @@ import pkg_resources from importlib_resources import path from benchmarks.benchmark_utils import swap_attr +from flatland.utils import graphics_pil + +graphics_pil.unattended_switch = True for entry in [entry for entry in importlib_resources.contents('examples') if not pkg_resources.resource_isdir('examples', entry) and entry.endswith(".py") and '__init__' not in entry + and 'example_basic_elements_test' not in entry and 'demo.py' not in entry ]: with path('examples', entry) as file_in: + print("") + print("") + print("") print("*****************************************************************") print("Running {}".format(entry)) print("*****************************************************************") diff --git a/flatland/utils/graphics_pil.py b/flatland/utils/graphics_pil.py index 2332ecf..22a5c5f 100644 --- a/flatland/utils/graphics_pil.py +++ b/flatland/utils/graphics_pil.py @@ -33,6 +33,9 @@ from flatland.core.grid.rail_env_grid import RailEnvTransitions # noqa: E402 class PILGL(GraphicsLayer): + # hack: in continuous integration, we run multiple + unattended_switch = False + def __init__(self, width, height, jupyter=False): self.yxBase = (0, 0) self.linewidth = 4 @@ -157,9 +160,14 @@ class PILGL(GraphicsLayer): def open_window(self): assert self.window_open is False, "Window is already open!" - # use tk.Toplevel() instead of tk.Tk() - # https://stackoverflow.com/questions/26097811/image-pyimage2-doesnt-exist - self.window_root = tk.Tk() + if self.unattended_switch: + # use tk.Toplevel() instead of tk.Tk() since we run all examples from the same python script + # https://stackoverflow.com/questions/26097811/image-pyimage2-doesnt-exist + tk.Toplevel() + else: + tk.Tk() + + self.window_root = tk.Toplevel() self.window_root.withdraw() self.window = tk.Toplevel(self.window_root) self.window.title("Flatland") diff --git a/tox.ini b/tox.ini index 3ff9c89..a64031d 100644 --- a/tox.ini +++ b/tox.ini @@ -97,7 +97,8 @@ commands = ; run examples from subfolder to ensure that resources are accessed via resources and not via relative paths sh -c 'mkdir -p {envtmpdir}/c236d3c240d61a0969d4cb59e2180ce5' ; pipe echo into python since some examples expect input to close the window after the example is run - sh -c 'cd {envtmpdir}/c236d3c240d61a0969d4cb59e2180ce5 && python {toxinidir}/benchmarks/run_all_examples.py' +; sh -c 'cd {envtmpdir}/c236d3c240d61a0969d4cb59e2180ce5 && python {toxinidir}/benchmarks/run_all_examples.py' + sh -c 'python {toxinidir}/benchmarks/run_all_examples.py' [testenv:notebooks] basepython = python -- GitLab