forked from PySimpleGUI/PySimpleGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo_Graph_Element_Sine_Wave.py
More file actions
56 lines (47 loc) · 1.86 KB
/
Demo_Graph_Element_Sine_Wave.py
File metadata and controls
56 lines (47 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
if sys.version_info[0] >= 3:
import PySimpleGUIWeb as sg
# import PySimpleGUIQt as sg
# import PySimpleGUI as sg
else:
import PySimpleGUI27 as sg
import math
SIZE_X = 200
SIZE_Y = 100
NUMBER_MARKER_FREQUENCY = 25
def draw_axis():
graph.DrawLine((-SIZE_X,0), (SIZE_X, 0)) # axis lines
graph.DrawLine((0,-SIZE_Y), (0,SIZE_Y))
for x in range(-SIZE_X, SIZE_X+1, NUMBER_MARKER_FREQUENCY):
graph.DrawLine((x,-3), (x,3)) # tick marks
if x != 0:
graph.DrawText( str(x), (x,-10), color='green') # numeric labels
for y in range(-SIZE_Y, SIZE_Y+1, NUMBER_MARKER_FREQUENCY):
graph.DrawLine((-3,y), (3,y))
if y != 0:
graph.DrawText( str(y), (-10,y), color='blue')
# Create the graph that will be put into the window
graph = sg.Graph(canvas_size=(400, 400),
graph_bottom_left=(-(SIZE_X+5), -(SIZE_Y+5)),
graph_top_right=(SIZE_X+5, SIZE_Y+5),
background_color='white',
key='graph')
# Window layout
layout = [[sg.Text('Example of Using Math with a Graph', justification='center', size=(50,1), relief=sg.RELIEF_SUNKEN)],
[graph],
[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_')]]
window = sg.Window('Graph of Sine Function', layout)
while True:
event, values = window.Read()
if event is None:
break
graph.Erase()
draw_axis()
prev_x = prev_y = None
for x in range(-SIZE_X,SIZE_X):
y = math.sin(x/int(values['_SLIDER2_']))*int(values['_SLIDER_'])
if prev_x is not None:
graph.DrawLine((prev_x, prev_y), (x,y), color='red')
prev_x, prev_y = x, y