# L-32 MCS 260 Fri 1 Apr 2016 : slidedot.py

"""
To get used to the screen coordinates,
we slide a dot along the canvas.
"""

from tkinter import Tk, Canvas, Scale, IntVar

class SlideDot(object):
    """
    GUI to demonstrate canvas coordinates.
    """
    def __init__(self, wdw, size):
        """
        determines the layout of the GUI
        """
        wdw.title('sliding a dot')
        self.dim = size
        self.cnv = Canvas(wdw, width=self.dim, \
            height=self.dim, bg='white')
        self.cnv.grid(row=1, column=0)
        self.xpos = IntVar()
        self.ypos = IntVar()
        self.low = 0
        self.high = self.dim
        self.hsx = Scale(wdw, \
            orient='horizontal', \
            from_=self.low, to=self.high, \
            tickinterval=(self.high-self.low)/10, \
            resolution=(self.high-self.low)/100, \
            length=self.dim, variable=self.xpos, \
        command=self.draw_dot)
        self.hsx.grid(row=0, column=0)
        self.hsx.set(self.dim/2)
        self.vsy = Scale(wdw, \
            orient='vertical', \
            from_=self.low, to=self.high, \
            tickinterval=(self.high-self.low)/10, \
            resolution=(self.high-self.low)/100, \
            length=self.dim, variable=self.ypos, \
            command=self.draw_dot)
        self.vsy.grid(row=1, column=1)
        self.vsy.set(self.dim/2)

    def draw_dot(self, val):
        """
        draws the dot and its scale variable
        """
        valx = self.xpos.get()
        valy = self.ypos.get()
        self.cnv.delete("dot", "text")
        txt = '(' + str(int(valx)) + ',' + str(int(valy)) + ')'
        self.cnv.create_text(valx, valy-10, text=txt, tags="text")
        self.cnv.create_oval(valx-6, valy-6, valx+6, valy+6, width=1, \
            outline='black', fill='SkyBlue2', tags="dot")

def main():
    """
    Instantiates the GUI and
    launches the main event loop.
    """
    top = Tk()
    SlideDot(top, 500)
    top.mainloop()

if __name__ == "__main__":
    main()
