From 04ee379b7d5e513583582f4898566b42ece17f82 Mon Sep 17 00:00:00 2001 From: u214892 <u214892@sbb.ch> Date: Thu, 4 Jul 2019 15:23:39 +0200 Subject: [PATCH] bugfix tk stuff --- benchmarks/benchmark_all_examples.py | 4 ---- benchmarks/profile_all_examples.py | 3 --- benchmarks/run_all_examples.py | 4 ---- flatland/utils/graphics_pil.py | 26 ++++++++------------------ flatland/utils/rendertools.py | 2 +- 5 files changed, 9 insertions(+), 30 deletions(-) diff --git a/benchmarks/benchmark_all_examples.py b/benchmarks/benchmark_all_examples.py index 5687ad8a..676bfe16 100644 --- a/benchmarks/benchmark_all_examples.py +++ b/benchmarks/benchmark_all_examples.py @@ -9,10 +9,6 @@ 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 b41f52a8..8015e23a 100644 --- a/benchmarks/profile_all_examples.py +++ b/benchmarks/profile_all_examples.py @@ -8,7 +8,6 @@ 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): @@ -25,8 +24,6 @@ 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 972ac68e..57dd6a93 100644 --- a/benchmarks/run_all_examples.py +++ b/benchmarks/run_all_examples.py @@ -7,15 +7,11 @@ 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: diff --git a/flatland/utils/graphics_pil.py b/flatland/utils/graphics_pil.py index 5f91b8bc..b5b761a2 100644 --- a/flatland/utils/graphics_pil.py +++ b/flatland/utils/graphics_pil.py @@ -33,8 +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 + # tk.Tk() must be a singleton! + # https://stackoverflow.com/questions/26097811/image-pyimage2-doesnt-exist + window = tk.Tk() def __init__(self, width, height, jupyter=False): self.yxBase = (0, 0) @@ -86,7 +87,6 @@ class PILGL(GraphicsLayer): self.ltAgentColors = [self.rgb_s2i(sColor) for sColor in sColors.split("#")] self.nAgentColors = len(self.ltAgentColors) - self.window_root = None self.window_open = False self.firstFrame = True self.old_background_image = (None, None, None) @@ -160,24 +160,14 @@ class PILGL(GraphicsLayer): def open_window(self): assert self.window_open is False, "Window is already open!" - 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 - self.window_root = tk.Toplevel() - else: - self.window_root = tk.Tk() - self.window_root.withdraw() - self.window = tk.Toplevel(self.window_root) - self.window.title("Flatland") - self.window.configure(background='grey') + self.__class__.window.title("Flatland") + self.__class__.window.configure(background='grey') self.window_open = True def close_window(self): self.panel.destroy() - self.window.quit() - self.window.destroy() - self.window_root.destroy() - self.window = None + # quit but not destroy! + self.__class__.window.quit() def text(self, *args, **kwargs): pass @@ -209,7 +199,7 @@ class PILGL(GraphicsLayer): self.panel.configure(image=tkimg) self.panel.image = tkimg - self.window.update() + self.__class__.window.update() self.firstFrame = False def pause(self, seconds=0.00001): diff --git a/flatland/utils/rendertools.py b/flatland/utils/rendertools.py index 7932aaca..8036294b 100644 --- a/flatland/utils/rendertools.py +++ b/flatland/utils/rendertools.py @@ -677,7 +677,7 @@ class RenderTool(object): if self.agentRenderVariant == AgentRenderVariant.BOX_ONLY: self.gl.setCellOccupied(iAgent, *(agent.position)) elif self.agentRenderVariant == AgentRenderVariant.ONE_STEP_BEHIND or \ - self.agentRenderVariant == AgentRenderVariant.ONE_STEP_BEHIND_AND_BOX: + self.agentRenderVariant == AgentRenderVariant.ONE_STEP_BEHIND_AND_BOX: if agent.old_position is not None: position = agent.old_position direction = agent.direction -- GitLab