Esempio n. 1
0
File: gui.py Progetto: wweng3/PyLogo
    def make_window(self, caption, model_gui_elements, screen_shape_width_height, bounce=True, fps=None):
        """
        Create the window, including sg.Graph, the drawing surface.
        """
        # --------------------- PySimpleGUI window layout and creation --------------------
        bounce_checkbox_line = ''
        if bounce is not None:
            bounce_checkbox_line = [sg.Checkbox('Bounce?', key='Bounce?', default=bounce,
                                    tooltip='Bounce back from the edges of the screen?')]

        fps_combo_line = ''
        if fps:
            fps_combo_line = [sg.Text('Frames/second', tooltip='The maximum frames/second.', pad=((0, 10), (10, 0))),
                              sg.Combo(key='fps', values=[1, 3, 6, 10, 15, 25, 40, 60],
                                       background_color='limegreen', default_value=fps,
                                       tooltip='The maximum frames/second.', pad=((0, 0), (10, 0)))
                              ]

        setup_go_line = [
            sg.Button(self.SETUP, pad=((0, 10), (10, 0))),
            sg.Button(self.GO_ONCE, disabled=True, button_color=('white', 'green'), pad=((0, 10), (10, 0))),
            sg.Button(self.GO, disabled=True, button_color=('white', 'green'), pad=((0, 30), (10, 0)),
                      key=self.GOSTOP)   ]


        exit_button_line = [sg.Exit(button_color=('white', 'firebrick4'), key=self.EXIT, pad=((70, 0), (10, 0)))]

        col1 = [ *model_gui_elements,
                 gui.HOR_SEP(),
                 setup_go_line,
                 bounce_checkbox_line,
                 fps_combo_line,
                 gui.HOR_SEP(),
                 exit_button_line
                 ]

        lower_left_pixel_xy = (0, screen_shape_width_height[1]-1)
        upper_right_pixel_xy = (screen_shape_width_height[0]-1, 0)
        col2 = [[sg.Graph(screen_shape_width_height, lower_left_pixel_xy, upper_right_pixel_xy,
                          background_color='black', key='-GRAPH-', enable_events=True)]]

        # layout is the actual layout of the window. The stuff above organizes it into component parts.
        # col1 is the control buttons, sliders, etc.
        # col2 is the screen on which the model is portrayed, i.e., the patches and the agents.
        # layout is a single "GUI line" with these two components in sequence.
        layout = [[sg.Column(col1), sg.Column(col2)]]

        window: sg.PySimpleGUI.Window = sg.Window(caption, layout, margins=(5, 20),
                                                  use_default_focus=False,
                                                  return_keyboard_events=True, finalize=True)
        graph: sg.PySimpleGUI.Graph = window['-GRAPH-']

        # -------------- Magic code to integrate PyGame with tkinter -------
        embed: tk.Canvas = graph.TKCanvas
        os.environ['SDL_WINDOWID'] = str(embed.winfo_id( ))
        os.environ['SDL_VIDEODRIVER'] = 'windib'  # change this to 'x11' to make it work on Linux

        return window
Esempio n. 2
0
    def make_window(self,
                    caption,
                    gui_left_upper,
                    gui_right_upper=None,
                    clear=None,
                    bounce=True,
                    fps=None):
        """
        Create the window, including sg.Graph, the drawing surface.
        """
        # --------------------- PySimpleGUI window layout and creation --------------------
        clear_line = [] if clear is None else \
                     [sg.Checkbox('Clear before setup?', key='Clear?', default=clear, pad=((0, 0), (10, 0)),
                                  tooltip='Bounce back from the edges of the screen?')]

        bounce_checkbox_line = [] if bounce is None else \
                               [sg.Checkbox('Bounce?', key='Bounce?', default=bounce, pad=((20, 0), (10, 0)),
                                            tooltip='Bounce back from the edges of the screen?')]

        clear_line += bounce_checkbox_line

        # Always an fps combo box, but make it visible only if the user specifies such a box.
        # The box is necessary to allow the program to set fps even if the end user doesn't
        fps_combo_line = [
            sg.Text('Frames/second',
                    tooltip='The maximum frames/second.',
                    visible=bool(fps),
                    pad=((0, 10), (17, 0))),
            sg.Combo(key=gui.FPS,
                     values=FPS_VALUES,
                     tooltip='The maximum frames/second.',
                     default_value=fps,
                     visible=bool(fps),
                     pad=((0, 0), (17, 0)),
                     enable_events=True)
        ]

        setup_go_line = [
            sg.Button(self.SETUP, pad=((0, 10), (10, 0))),
            sg.Button(gui.GO_ONCE,
                      disabled=True,
                      button_color=('white', 'green'),
                      pad=((0, 10), (10, 0))),
            sg.Button(gui.GO,
                      disabled=True,
                      button_color=('white', 'green'),
                      pad=((0, 30), (10, 0)),
                      key=gui.GOSTOP)
        ]

        exit_button_line = [
            sg.Exit(button_color=('white', 'firebrick4'),
                    key=self.EXIT,
                    pad=((0, 0), (10, 0))),
            sg.Checkbox('Grab anywhere',
                        key='Grab',
                        default=False,
                        pad=((40, 0), (10, 0)))
        ]

        col1 = [
            *gui_left_upper,
            gui.HOR_SEP(), setup_go_line, clear_line, fps_combo_line,
            gui.HOR_SEP(), exit_button_line
        ]

        lower_left_pixel_xy = (0, self.screen_shape_width_height[1] - 1)
        upper_right_pixel_xy = (self.screen_shape_width_height[0] - 1, 0)

        if gui_right_upper is None:
            gui_right_upper = [[]]

        # graph is a drawing area, a screen on which the model is portrayed, i.e., the patches and the agents.
        # It consists mainly of a TKCanvas.
        graph = sg.Graph(self.screen_shape_width_height,
                         lower_left_pixel_xy,
                         upper_right_pixel_xy,
                         background_color='black',
                         key='-GRAPH-',
                         enable_events=True,
                         drag_submits=True)
        col2 = gui_right_upper + [[graph]]

        # layout is the actual layout of the window. The stuff above organizes it into component parts.
        # col1 is the control buttons, sliders, etc.
        # col2 is the graph plus whatever the user wants to put above it.
        # layout is a single "GUI line" with these two components in sequence.
        layout = [[sg.Column(col1), sg.Column(col2)]]

        # window is a window with that layout.
        window = sg.Window(caption,
                           layout,
                           margins=(5, 20),
                           use_default_focus=False,
                           grab_anywhere=False,
                           return_keyboard_events=True,
                           finalize=True)

        # -------------- Magic code to integrate PyGame with tkinter -------
        w_id = graph.TKCanvas.winfo_id()
        os.environ['SDL_WINDOWID'] = str(w_id)
        os.environ[
            'SDL_VIDEODRIVER'] = 'windib'  # change this to 'x11' to make it work on Linux

        return window