# L-32 MCS 260 Fri 9 Nov 2007 : slidedot.py # # To get used to the screen coordinates, # we slide a dot along the canvas. from Tkinter import * class SlideDot(): """ GUI to demonstrate canvas coordinates. """ def __init__(self,wdw): "determines the layout of the GUI" wdw.title('sliding a dot') self.d = 400 self.c = Canvas(wdw,width=self.d,\ height=self.d,bg ='white') self.c.grid(row=1,column=0) self.x = IntVar() self.y = IntVar() self.L = 0; self.H = self.d self.sx = Scale(wdw,\ orient='horizontal',\ from_=self.L,to=self.H,\ tickinterval=(self.H-self.L)/10,\ resolution=(self.H-self.L)/100,\ length=self.d,variable=self.x,\ command=self.DrawCircle) self.sx.grid(row=0,column=0) self.sx.set(self.d/2) self.sy = Scale(wdw,\ orient='vertical',\ from_=self.L,to=self.H,\ tickinterval=(self.H-self.L)/10,\ resolution=(self.H-self.L)/100,\ length=self.d,variable=self.y,\ command=self.DrawCircle) self.sy.grid(row=1,column=1) self.sy.set(self.d/2) def DrawCircle(self,v): "draws the dot and its scale variable" vx = self.x.get() vy = self.y.get() self.c.delete("dot","text") t = '(' + str(int(vx)) + ',' + str(int(vy)) + ')' self.c.create_text(vx,vy-10,text=t,tags="text") self.c.create_oval(vx-6,vy-6,vx+6,vy+6,width=1,\ outline='black',fill='SkyBlue2',tags="dot") def main(): top = Tk() show = SlideDot(top) top.mainloop() if __name__ == "__main__": main()