2018-09-27 20:24:09 +00:00
|
|
|
#!/usr/bin/env python
|
2019-10-23 20:10:03 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-09-18 01:59:11 +00:00
|
|
|
import random
|
2018-09-24 22:01:00 +00:00
|
|
|
import sys
|
2018-09-18 01:59:11 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
'''
|
|
|
|
Example of random line in Graph element.
|
|
|
|
'''
|
|
|
|
|
2019-12-15 17:14:55 +00:00
|
|
|
sg.change_look_and_feel('black')
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
STEP_SIZE = 1
|
2018-09-18 01:59:11 +00:00
|
|
|
SAMPLES = 300
|
|
|
|
SAMPLE_MAX = 300
|
2019-10-23 20:10:03 +00:00
|
|
|
CANVAS_SIZE = (300, 300)
|
2018-09-18 01:59:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
global g_exit, g_response_time
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
layout = [[sg.Text('Enter width, height of graph')],
|
2019-12-15 17:14:55 +00:00
|
|
|
[sg.Input(300, size=(6, 1), key='w'), sg.Input(300, size=(6, 1), key='h')],
|
2018-09-24 22:01:00 +00:00
|
|
|
[sg.Ok(), sg.Cancel()]]
|
2018-09-18 01:59:11 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('Enter graph size', layout)
|
|
|
|
event, values = window.read()
|
|
|
|
if event is None or event == 'Cancel':
|
|
|
|
return
|
|
|
|
|
|
|
|
CANVAS_SIZE = int(values['w']), int(values['h'])
|
|
|
|
window.close()
|
2018-09-18 01:59:11 +00:00
|
|
|
|
|
|
|
# start ping measurement thread
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
sg.change_look_and_feel('Black')
|
|
|
|
sg.set_options(element_padding=(0, 0))
|
2018-09-18 01:59:11 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
layout = [[sg.Button('Quit', button_color=('white', 'black'))],
|
2019-12-15 17:14:55 +00:00
|
|
|
[sg.Graph(CANVAS_SIZE, (0, 0), (SAMPLES, SAMPLE_MAX),
|
2019-10-23 20:10:03 +00:00
|
|
|
background_color='black', key='graph')], ]
|
2018-09-18 01:59:11 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('Canvas test', layout, grab_anywhere=True,
|
|
|
|
background_color='black', no_titlebar=False,
|
|
|
|
use_default_focus=False, finalize=True)
|
2019-12-15 17:14:55 +00:00
|
|
|
graph = window['graph'] # type:sg.Graph
|
|
|
|
|
|
|
|
graph.draw_line((SAMPLES//2, 0), (SAMPLES//2,SAMPLE_MAX),color='white')
|
|
|
|
graph.draw_line((0,SAMPLE_MAX//2), (SAMPLES, SAMPLE_MAX//2),color='white')
|
2018-09-18 01:59:11 +00:00
|
|
|
|
|
|
|
prev_response_time = None
|
2019-10-23 20:10:03 +00:00
|
|
|
i = 0
|
2018-09-18 01:59:11 +00:00
|
|
|
prev_x, prev_y = 0, 0
|
|
|
|
graph_value = 250
|
2019-12-15 17:14:55 +00:00
|
|
|
figures = []
|
2018-09-18 01:59:11 +00:00
|
|
|
while True:
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read(timeout=0)
|
2018-10-29 00:01:03 +00:00
|
|
|
if event == 'Quit' or event is None:
|
2018-09-18 01:59:11 +00:00
|
|
|
break
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2018-09-18 01:59:11 +00:00
|
|
|
graph_offset = random.randint(-10, 10)
|
|
|
|
graph_value = graph_value + graph_offset
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2018-09-18 01:59:11 +00:00
|
|
|
if graph_value > SAMPLE_MAX:
|
|
|
|
graph_value = SAMPLE_MAX
|
|
|
|
if graph_value < 0:
|
|
|
|
graph_value = 0
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2018-09-18 01:59:11 +00:00
|
|
|
new_x, new_y = i, graph_value
|
|
|
|
prev_value = graph_value
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2018-09-18 01:59:11 +00:00
|
|
|
if i >= SAMPLES:
|
2019-12-15 17:14:55 +00:00
|
|
|
graph.delete_figure(figures[0])
|
|
|
|
figures = figures[1:]
|
|
|
|
for count, figure in enumerate(figures):
|
|
|
|
graph.move_figure(figure, -STEP_SIZE, 0)
|
2018-09-18 01:59:11 +00:00
|
|
|
prev_x = prev_x - STEP_SIZE
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2019-12-15 17:14:55 +00:00
|
|
|
# print(f'io={i} {prev_x,prev_y} to {new_x, new_y}')
|
|
|
|
last_figure = graph.draw_line((prev_x, prev_y), (new_x, new_y), color='white')
|
|
|
|
figures.append(last_figure)
|
2018-09-18 01:59:11 +00:00
|
|
|
prev_x, prev_y = new_x, new_y
|
|
|
|
i += STEP_SIZE if i < SAMPLES else 0
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window.close()
|
2018-09-18 01:59:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|