Skip to content

Instantly share code, notes, and snippets.

@psqq
Created June 23, 2017 09:08
Show Gist options
  • Save psqq/281926f2aea22e3a9b316f68ac64c9d0 to your computer and use it in GitHub Desktop.
Save psqq/281926f2aea22e3a9b316f68ac64c9d0 to your computer and use it in GitHub Desktop.
from Tkinter import *
import math
def draw_sin(a1, a2):
c.delete("all")
center = height//2
x_start = a1
x_max = width
x_increment = 1
x_factor = (a2 - a1) / 400
y_amplitude = 80
center_line = c.create_line(0, center, width, center, fill='green')
xy1 = []
for x in range(400):
xy1.append(x_start + x * x_increment)
xy1.append(int(math.sin(x_start + x * x_factor) * y_amplitude) + center)
sin_line = c.create_line(xy1, fill='blue')
def entry_changed(*args):
draw_sin(a1.get(), a2.get())
root = Tk()
root.title("Simple plot using canvas and line")
a1 = DoubleVar()
a1.set(0)
a1.trace("w", entry_changed)
textl = Entry(root, textvariable=a1)
textl.pack()
a2 = DoubleVar()
a2.set(2 * math.pi)
a2.trace("w", entry_changed)
textr = Entry(root, textvariable=a2)
textr.pack()
width = 400
height = 300
c = Canvas(width=width, height=height, bg='white')
c.pack()
str1 = "sin(x)=blue"
c.create_text(10, 20, anchor=SW, text=str1)
entry_changed()
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment