# L-22.5 MCS 260 Wed 6 Aug 2014 : guipin.py

"""
We use four scales to enter a PIN number.
The PIN number is confirmed in an Entry widget.
"""

from Tkinter import Tk, IntVar, Scale
from Tkinter import Entry, W, E, END, INSERT

class GuiPin(object):
    """
    Four scales to enter a 4-digit number.
    """
    def __init__(self, wdw):
        """
        Defines four scales and one entry.
        """
        wdw.title('PIN slider')
        self.dgt0 = IntVar()
        self.dgt1 = IntVar()
        self.dgt2 = IntVar()
        self.dgt3 = IntVar()
        self.scl0 = Scale(wdw, orient='vertical', \
            from_=0, to=9, resolution=1, \
            variable=self.dgt0, command=self.show_pin)
        self.scl1 = Scale(wdw, orient='vertical', \
            from_=0, to=9, resolution=1, \
            variable=self.dgt1, command=self.show_pin)
        self.scl2 = Scale(wdw, orient='vertical', \
            from_=0, to=9, resolution=1, \
            variable=self.dgt2, command=self.show_pin)
        self.scl3 = Scale(wdw, orient='vertical', \
            from_=0, to=9, resolution=1, \
            variable=self.dgt3, command=self.show_pin)
        self.scl3.grid(row=0, column=0)
        self.scl2.grid(row=0, column=1)
        self.scl1.grid(row=0, column=2)
        self.scl0.grid(row=0, column=3)
        self.ent = Entry(wdw)
        self.ent.grid(row=1, columnspan=4, sticky=W+E)

    def show_pin(self, val):
        """
        Puts the PIN in the entry widget
        """
        nbr0 = self.dgt0.get()
        nbr1 = self.dgt1.get()
        nbr2 = self.dgt2.get()
        nbr3 = self.dgt3.get()
        nbr = nbr0 + 10*(nbr1 + 10*(nbr2 + 10*nbr3))
        shn = '%04d' % nbr
        self.ent.delete(0, END)
        self.ent.insert(INSERT, shn)

def main():
    """
    Instantiates the GUI and launches
    the main event loop.
    """
    top = Tk()
    show = GuiPin(top)
    top.mainloop()

if __name__ == "__main__":
    main()
