# L-9 MCS 275 Mon 1 Feb 2010 : multigon.py from Tkinter import * import math class MultiGon(): """ GUI to draw a regular n-gon on canvas. """ def __init__(self,wdw): "determines the layout of the GUI" wdw.title('regular n-gon') self.d = 400 self.n = IntVar() self.sn = Scale(wdw,orient='horizontal',\ from_=1,to=20,tickinterval=1,\ length=self.d,variable=self.n,\ command=self.DrawGon) self.sn.set(10) self.sn.grid(row=0,column=0) self.c = Canvas(wdw,width=self.d,\ height=self.d,bg ='white') self.c.grid(row=1,column=0) def DrawGon(self,v): "Draws a regular n-gon" cx = self.d/2 cy = self.d/2 radius = 0.4*self.d self.c.delete(ALL) self.c.create_text(cx,cy,text=v,tags="text") n = int(v) L = [] for i in range(0,n): vx = cx + radius*math.cos(2*i*math.pi/n) vy = cy + radius*math.sin(2*i*math.pi/n) self.c.create_oval(vx-6,vy-6,vx+6,vy+6,width=1,\ outline='black',fill='SkyBlue2',tags="dot") L.append((vx,vy)) for i in range(0,n-1): self.c.create_line(L[i][0],L[i][1],L[i+1][0],L[i+1][1],width=2) self.c.create_line(L[n-1][0],L[n-1][1],L[0][0],L[0][1],width=2) def main(): top = Tk() show = MultiGon(top) top.mainloop() if __name__ == "__main__": main()