def main():
    global g_interval,  g_procs, g_exit

    # ----------------  Create Form  ----------------
    sg.change_look_and_feel('Black')
    layout = [[sg.Text('', size=(8, 1), font=('Helvetica', 20), text_color=sg.YELLOWS[0],
                    justification='center', key='text')],
              [sg.Text('', size=(30, 8), font=('Courier New', 12),
                    text_color='white', justification='left', key='processes')],
              [sg.Exit(button_color=('white', 'firebrick4'),
                       pad=((15, 0), 0), size=(9, 1)), ]
              ]

    window = sg.Window('CPU Utilization', layout,
           no_titlebar=True, keep_on_top=True, alpha_channel=.8, grab_anywhere=True)

    # start cpu measurement thread
    thread = Thread(target=CPU_thread, args=(None,), daemon=True)
    thread.start()
    timeout_value = 1             # make first read really quick
    g_interval = 1
    # ----------------  main loop  ----------------
    while True:
        # --------- Read and update window --------
        event, values = window.read(
            timeout=timeout_value, timeout_key='Timeout')
        # --------- Do Button Operations --------
        if event in (None, 'Exit'):
            break

        timeout_value = 1000

        cpu_percent = g_cpu_percent
        display_string = ''
        if g_procs:
            # --------- Create list of top % CPU porocesses --------
            try:
                top = {proc.name(): proc.cpu_percent() for proc in g_procs}
            except:
                pass

            top_sorted = sorted(
                top.items(), key=operator.itemgetter(1), reverse=True)
            if top_sorted:
                top_sorted.pop(0)
            display_string = ''
            for proc, cpu in top_sorted:
                display_string += '{:2.2f} {}\n'.format(cpu/10, proc)

        # --------- Display timer and proceses in window --------
        window['text'].update('CPU {}'.format(cpu_percent))
        window['processes'].update(display_string)

    window.close()
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()
Example #3
0
#!/usr/bin/env python
import PySimpleGUIWeb as sg
import time

# ----------------  Create Form  ----------------
layout = [
    [sg.Text('', background_color='black')],
    [sg.Text('test', size=(20, 1), font=('Helvetica', 30), justification='center', text_color='white', key='text', background_color='black')],
    [sg.Text('', background_color='black')],
    [sg.Button('Pause', key='button', button_color=('white', '#001480')),
     sg.Button('Reset', button_color=('white', '#007339'), key='Reset'),
     sg.Exit(button_color=('white', '#8B1A1A'), key='Exit')]
        ]

window = sg.Window('Running Timer', background_color='black', font='Helvetica 18').Layout(layout)

# ----------------  main loop  ----------------
current_time = 0
paused = False
start_time = int(round(time.time() * 100))
while (True):
    # --------- Read and update window --------
    if not paused:
        event, values = window.Read(timeout=0)
        current_time = int(round(time.time() * 100)) - start_time
    else:
        event, values = window.Read()
    if event == 'button':
        event = window.FindElement(event).GetText()
    # --------- Do Button Operations --------
    if event is None or event == 'Exit':        # ALWAYS give a way out of program
Example #4
0
def main():

    layout = [[sg.Text('Process Killer - Choose one or more processes',
                    size=(45, 1), font=('Helvetica', 15), text_color='red')],

              [sg.Listbox(values=[' '], size=(50, 30),
                          select_mode=sg.SELECT_MODE_EXTENDED,  font=('Courier', 12), key='_processes_')],

              [sg.Text('Click refresh once or twice.. once for list, second to get CPU usage')],
              [sg.Text('Filter by typing name', font='ANY 14'), sg.Input(
                  size=(15, 1), font='any 14', key='_filter_')],

              [sg.Button('Sort by Name', ),
               sg.Button('Sort by % CPU', button_color=('white', 'DarkOrange2')),
               sg.Button('Kill', button_color=('white', 'red'), bind_return_key=True),
               sg.Exit(button_color=('white', 'sea green'))]]

    window = sg.Window('Process Killer', layout,
                       keep_on_top=True, auto_size_buttons=False,
                       default_button_element_size=(12, 1), return_keyboard_events=True)

    display_list = None
    # ----------------  main loop  ----------------
    while True:
        # --------- Read and update window --------
        event, values = window.Read()
        if event in (None, 'Exit'):
            break

        # skip mouse, control key and shift key events entirely
        if 'Mouse' in event or 'Control' in event or 'Shift' in event:
            continue
        print(event, values)
        # --------- Do Button Operations --------
        if event == 'Sort by Name':
            psutil.cpu_percent(interval=.1)
            procs = psutil.process_iter()
            all_procs = [[proc.cpu_percent(), proc.name(), proc.pid]
                         for proc in procs]
            sorted_by_cpu_procs = sorted(
                all_procs, key=operator.itemgetter(1), reverse=False)
            display_list = [
                '{:5d} {:5.2f} {}\n'.format(
                    process[2], process[0] / 10, process[1]
                )
                for process in sorted_by_cpu_procs
            ]

            window['_processes_'].update(display_list)
            print(display_list)
        elif event == 'Kill':
            processes_to_kill = values['_processes_']
            for proc in processes_to_kill:
                pid = int(proc[0:5])
                # if sg.popupYesNo('About to kill {} {}'.format(pid, proc[12:]), keep_on_top=True) == 'Yes':
                try:
                    kill_proc_tree(pid=pid)
                except:
                    sg.popup_auto_close(
                        'Error killing process', auto_close_duration=1)
        elif event == 'Sort by % CPU':
            psutil.cpu_percent(interval=.1)
            procs = psutil.process_iter()
            all_procs = [[proc.cpu_percent(), proc.name(), proc.pid]
                         for proc in procs]
            sorted_by_cpu_procs = sorted(
                all_procs, key=operator.itemgetter(0), reverse=True)
            display_list = [
                '{:5d} {:5.2f} {}\n'.format(
                    process[2], process[0] / 10, process[1]
                )
                for process in sorted_by_cpu_procs
            ]

            window['_processes_'].update(display_list)
        else:           # was a typed character
            if display_list is not None:
                new_output = [
                    line
                    for line in display_list
                    if values['_filter_'] in line.lower()
                ]

                window['_processes_'].update(new_output)
    window.close()