#Sinewheel hip-pocket program in Python/Tkinter #William Baker, Summer 2004, illiMath04 Program #Adapted from Math198 course notes by Prof. George Francis #University of Illinois Urbana-Champaign Dept. of Mathematics #Edited by GF 19sep09 import Tkinter # 2D drawing package for Python import math # all the math you'll likely need import time # for pauses and delays defaldelay=4 #default delay time controls animation speed #createLine() #pronounce "creatLine function" #Draws a red line segment between the specified coordinates def createLine(x0, y0, x1, y1): canvas.create_line(x0, y0, x1, y1, fill="red") #startAnimation() #Performs the animation when called by the clicking start button #Translate the BASIC program into this function def startAnimation(): dg = .01745 # one degree in radians dx = 6 # step size r = 75 # circle radius x0=80 # location of origin y0=240 # location of origin x=0 # arclength parameter for x in range(0, 361, dx): xr = x0+r*math.cos(x*dg) # end of radius on circle yr = y0-r*math.sin(x*dg) xx = x0+r+r*x*dg # mystery variable createLine(x0,y0,xr,yr) createLine(xr,yr,xx,yr) createLine(xx,yr,xx,y0) canvas.update() # swap buffers time.sleep(int(timedelay.get())/20.0) # slow animation #clear() #Clears the cavas, called when clear button is clicked def clear(): canvas.delete("all") # Implementation of the above program in TkInter window = Tkinter.Tk() window.title("Sinewheel in Python/Tkinter") clear_button = Tkinter.Button(window, text = "Clear", command = clear) start = Tkinter.Button(window, text="Start", command=startAnimation) delLabel=Tkinter.Label(window, width="15") delLabel.configure(text="Time Delay= ") timedelay=Tkinter.StringVar() timedelay.set(`defaldelay`) delentry = Tkinter.Entry(window, width="15", textvariable=timedelay) canvas = Tkinter.Canvas(window, width=640, height=480) canvas.pack(side="top") delLabel.pack(side="left") delentry.pack(side="left") start.pack(side="right") clear_button.pack(side = "right") # start event loop window.mainloop()