2019-09-25 19:24:54 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-09-21 04:45:21 +00:00
|
|
|
import math
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
# Yet another usage of Graph element.
|
|
|
|
|
2019-04-20 22:39:59 +00:00
|
|
|
SIZE_X = 200
|
|
|
|
SIZE_Y = 100
|
|
|
|
NUMBER_MARKER_FREQUENCY = 25
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2019-04-20 22:39:59 +00:00
|
|
|
def draw_axis():
|
2019-10-23 20:10:03 +00:00
|
|
|
graph.draw_line((-SIZE_X, 0), (SIZE_X, 0)) # axis lines
|
|
|
|
graph.draw_line((0, -SIZE_Y), (0, SIZE_Y))
|
2019-04-20 22:39:59 +00:00
|
|
|
|
|
|
|
for x in range(-SIZE_X, SIZE_X+1, NUMBER_MARKER_FREQUENCY):
|
2019-10-23 20:10:03 +00:00
|
|
|
graph.draw_line((x, -3), (x, 3)) # tick marks
|
2019-04-20 22:39:59 +00:00
|
|
|
if x != 0:
|
2019-10-23 20:10:03 +00:00
|
|
|
# numeric labels
|
|
|
|
graph.draw_text(str(x), (x, -10), color='green')
|
2019-04-20 22:39:59 +00:00
|
|
|
|
|
|
|
for y in range(-SIZE_Y, SIZE_Y+1, NUMBER_MARKER_FREQUENCY):
|
2019-10-23 20:10:03 +00:00
|
|
|
graph.draw_line((-3, y), (3, y))
|
2019-04-20 22:39:59 +00:00
|
|
|
if y != 0:
|
2019-10-23 20:10:03 +00:00
|
|
|
graph.draw_text(str(y), (-10, y), color='blue')
|
|
|
|
|
2019-12-24 23:52:47 +00:00
|
|
|
sg.theme('DarkAmber')
|
2019-04-20 22:39:59 +00:00
|
|
|
|
|
|
|
# Create the graph that will be put into the window
|
|
|
|
graph = sg.Graph(canvas_size=(400, 400),
|
2019-10-23 20:10:03 +00:00
|
|
|
graph_bottom_left=(-(SIZE_X+5), -(SIZE_Y+5)),
|
|
|
|
graph_top_right=(SIZE_X+5, SIZE_Y+5),
|
|
|
|
background_color='white',
|
|
|
|
key='graph')
|
2019-04-20 22:39:59 +00:00
|
|
|
# Window layout
|
2019-10-23 20:10:03 +00:00
|
|
|
layout = [[sg.Text('Example of Using Math with a Graph', justification='center', size=(50, 1), relief=sg.RELIEF_SUNKEN)],
|
2019-04-20 22:39:59 +00:00
|
|
|
[graph],
|
2019-10-23 20:10:03 +00:00
|
|
|
[sg.Text('y = sin(x / x2 * x1)', font='COURIER 18')],
|
|
|
|
[sg.Text('x1'), sg.Slider((0, 200), orientation='h',
|
|
|
|
enable_events=True, key='-SLIDER-')],
|
|
|
|
[sg.Text('x2'), sg.Slider((1, 200), orientation='h', enable_events=True, key='-SLIDER2-')]]
|
2019-04-20 22:39:59 +00:00
|
|
|
|
2019-04-30 23:35:50 +00:00
|
|
|
window = sg.Window('Graph of Sine Function', layout)
|
2019-04-20 22:39:59 +00:00
|
|
|
|
|
|
|
while True:
|
2019-10-13 19:14:17 +00:00
|
|
|
event, values = window.read()
|
2020-05-07 10:22:59 +00:00
|
|
|
if event == sg.WIN_CLOSED:
|
2019-04-20 22:39:59 +00:00
|
|
|
break
|
2019-10-13 19:14:17 +00:00
|
|
|
graph.erase()
|
2019-04-20 22:39:59 +00:00
|
|
|
draw_axis()
|
|
|
|
prev_x = prev_y = None
|
2019-10-23 20:10:03 +00:00
|
|
|
|
|
|
|
for x in range(-SIZE_X, SIZE_X):
|
|
|
|
y = math.sin(x/int(values['-SLIDER2-']))*int(values['-SLIDER-'])
|
2019-04-20 22:39:59 +00:00
|
|
|
if prev_x is not None:
|
2019-10-23 20:10:03 +00:00
|
|
|
graph.draw_line((prev_x, prev_y), (x, y), color='red')
|
2019-04-20 22:39:59 +00:00
|
|
|
prev_x, prev_y = x, y
|
2018-09-21 04:45:21 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window.close()
|