def main():

    fig_dict = {
        'Pyplot Simple': PyplotSimple,
        'Pyplot Formatstr': PyplotFormatstr,
        'PyPlot Three': Subplot3d,
        'Unicode Minus': UnicodeMinus,
        'Pyplot Scales': PyplotScales,
        'Axes Grid': AxesGrid,
        'Exploring Normalizations': ExploringNormalizations,
        'Different Scales': DifferentScales,
        'Pyplot Box Plot': PyplotBoxPlot,
        'Pyplot ggplot Style Sheet': PyplotGGPlotSytleSheet,
        'Pyplot Line Poly Collection': PyplotLinePolyCollection,
        'Pyplot Line Styles': PyplotLineStyles,
        'Pyplot Scatter With Legend': PyplotScatterWithLegend,
        'Artist Customized Box Plots': PyplotArtistBoxPlots,
        'Artist Customized Box Plots 2': ArtistBoxplot2,
        'Pyplot Histogram': PyplotHistogram
    }

    # define the layout

    col_listbox = [[
        sg.Listbox(values=fig_dict.keys(),
                   enable_events=True,
                   size=(25, len(fig_dict.keys())),
                   key='-LISTBOX-')
    ], [sg.Exit(size=(5, 2))]]

    layout = [
        [sg.Text('Matplotlib Plot Browser', font=('current 25'))],
        [
            sg.Text(
                'Choose a plot from the list to see the plot and the source code used to make it.'
            )
        ],
        [
            sg.Column(col_listbox, element_justification='c'),
            sg.Image(key='-IMAGE-'),
            sg.MLine(size=(40, 20), font='Courier 12', key='-MULTILINE-')
        ],
    ]

    # create the window
    window = sg.Window('Embedding Matplotlib In PySimpleGUIWeb', layout)

    while True:  # The event loop
        event, values = window.read()
        # print(event, values)                  # helps greatly when debugging
        if event in (sg.WIN_CLOSED,
                     'Exit'):  # if user closed window or clicked Exit button
            break

        choice = values['-LISTBOX-'][
            0]  # get first listbox item chosen (returned as a list)
        the_plotting_func = fig_dict[
            choice]  # get function to call from the dictionary
        window['-MULTILINE-'].update(inspect.getsource(
            the_plotting_func))  # show source code to function in multiline
        draw_figure(the_plotting_func(), window['-IMAGE-'])  # draw the figure
    window.close()
Esempio n. 2
0
def HowDoI():
    '''
    Make and show a window (PySimpleGUI form) that takes user input and sends to the HowDoI web oracle
    Excellent example of 2 GUI concepts
        1. Output Element that will show text in a scrolled window
        2. Non-Window-Closing Buttons - These buttons will cause the form to return with the form's values, but doesn't close the form
    :return: never returns
    '''
    # -------  Make a new Window  ------- #
    sg.change_look_and_feel('GreenTan')            # give our form a spiffy set of colors

    layout =  [
                [sg.Text('Ask and your answer will appear here....', size=(40, 1))],
                [sg.MLineOutput(size_px=(980, 400),key='_OUTPUT_' )],
                # [ sg.Spin(values=(1, 2, 3, 4), initial_value=1, size=(2, 1), key='Num Answers', font='Helvetica 15'),
                [ sg.CBox('Display Full Text', key='full text', font='Helvetica 15'),
                sg.Text('Command History', font='Helvetica 15'), sg.Text('', size=(40,3), text_color=sg.BLUES[0], key='history')],
                [sg.MLine(size=(85, 5), enter_submits=True, key='query', do_not_clear=False),
                sg.ReadButton('SEND', button_color=(sg.YELLOWS[0], sg.BLUES[0]), bind_return_key=True),
                sg.Button('EXIT', button_color=(sg.YELLOWS[0], sg.GREENS[0]))]
              ]

    window = sg.Window('How Do I?', layout, default_element_size=(30,1),
            font=('Helvetica',' 17'), default_button_element_size=(8,2),
            return_keyboard_events=False)
    
    # ---===--- Loop taking in user input and using it to query HowDoI --- #
    command_history = []
    history_offset = 0
    while True:

        event, values = window.Read()
        # print(event, values)
        if type(event) is int:
            event = str(event)
        if event == 'SEND':
            query = values['query'].rstrip()
            window['_OUTPUT_'].update(query, append=True)
            print(query)
            QueryHowDoI(query, 1, values['full text'], window)  # send the string to HowDoI
            command_history.append(query)
            history_offset = len(command_history)-1

            # manually clear input because keyboard events blocks clear
            window['query'].update('')
            window['history'].update('\n'.join(command_history[-3:]))

        # if exit button or closed using X
        elif event == None or event == 'EXIT':
            break

        # scroll back in history
        elif 'Up' in event and len(command_history):
            command = command_history[history_offset]

            # decrement is not zero
            history_offset -= 1 * (history_offset > 0)
            window['query'].update(command)

        # scroll forward in history
        elif 'Down' in event and len(command_history):

            # increment up to end of list
            history_offset += 1 * (history_offset < len(command_history)-1)
            command = command_history[history_offset]
            window['query'].update(command)

        # clear currently line
        elif 'Escape' in event:
            window['query'].update('')

    window.close()