Changeset 98

Show
Ignore:
Timestamp:
04/06/08 13:10:38 (5 months ago)
Author:
nlawren2
Message:

Added tabs for the TkniterUI

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sucrose/trunk/python/api/sucrose.py

    r84 r98  
    3737                return self.trays.filter_by(machine_tray_id=int(location)).first() 
    3838 
    39         def vend(self, location, uin): 
    40                 transaction = self.session.begin() 
    41                 try: 
    42                         user = self.users.filter_by(uin=uin).first() 
    43                         if not user: 
    44                                 raise ValueError('no such user') 
     39       def vend(self, location, uin): 
     40               transaction = self.session.begin() 
     41               try: 
     42                       user = self.users.filter_by(uin=uin).first() 
     43                       if not user: 
     44                               raise ValueError('no such user') 
    4545 
    46                         tray = self.tray_from_location(location) 
    47                         if not tray: 
    48                                 raise ValueError('no such tray!?') 
    49                         if not tray.item_id: 
    50                                 raise ValueError('no item in tray') 
    51                         if tray.quantity <= 0: 
    52                                 raise ValueError('no product in tray') 
     46                       tray = self.tray_from_location(location) 
     47                       if not tray: 
     48                               raise ValueError('no such tray!?') 
     49                       if not tray.item_id: 
     50                               raise ValueError('no item in tray') 
     51                       if tray.quantity <= 0: 
     52                               raise ValueError('no product in tray') 
    5353 
    54                         # comparing Decimal with float will otherwise behave strangely 
    55                         if tray.cur_price * 100 > int(user.balance * 100): 
    56                                 raise ValueError('not enough money') 
     54                       # comparing Decimal with float will otherwise behave strangely 
     55                       if tray.cur_price * 100 > int(user.balance * 100): 
     56                               raise ValueError('not enough money') 
    5757 
    58                         tray.quantity -= 1 
    59                         tray.num_consumed += 1 
    60                         user.balance -= float(tray.cur_price) 
    61                         txn = self.db.Transaction(user_id=user.uid, item_id=tray.item_id, cost=tray.cur_price) 
    62                         self.session.save(txn) 
    63                         if hasattr(self, 'pic'): 
    64                                 self.pic.vendItem(location) 
    65                         self.session.commit() 
    66                 except: 
    67                         self.session.rollback() 
    68                         raise 
     58                       tray.quantity -= 1 
     59                       tray.num_consumed += 1 
     60                       user.balance -= float(tray.cur_price) 
     61                       txn = self.db.Transaction(user_id=user.uid, item_id=tray.item_id, cost=tray.cur_price) 
     62                       self.session.save(txn) 
     63                       if hasattr(self, 'pic'): 
     64                               self.pic.vendItem(location) 
     65                               self.session.commit() 
     66               except: 
     67                       self.session.rollback() 
     68                       raise 
    6969 
    7070        def item_name_from_id(self, id): 
  • sucrose/trunk/python/keypad_tk.py

    r78 r98  
    1010""" 
    1111 
    12 class Keypad(user_interface.UserInterface): 
    13  
    14         def make_button_callback(self,number): 
    15                 return lambda: self.button_callback(number) 
    16          
    17          
    18         def button_callback(self,number): 
    19                 self.location=self.location[1]+str(number) 
    20                 self.location_lbl.config(text=self.location) 
    21                 self.name_lbl.config(text="") 
    22                  
    23                 if self.auth.authenticate_purchase(self.location): 
    24                         self.vend.config(state=Tkinter.NORMAL) 
    25                 else: 
    26                         # If it's not a valid purchase, don't allow the user to press the  
    27                         # vend button 
    28                         self.vend.config(state=Tkinter.DISABLED) 
     12class TkinterUI(user_interface.UserInterface): 
     13        def make_switch_view(self, view): 
     14                return lambda: self.switch_view(view) 
    2915 
    3016 
    31         def reset(self): 
    32                 if self.location == "00": # If the location is 00, quit 
    33                         self.destruct() 
    34                 else:   # Reset the display 
    35                         self.location="00" 
    36                         self.location_lbl.config(text=self.location) 
    37                         self.name_lbl.config(text="") 
     17        def make_view(self, view_name, view): 
     18                setattr(self,view_name, view) 
     19                self.views.add_command(label=view_name, command=self.make_switch_view(view)) 
     20                self.view_names.append(view_name) 
     21                self.current_view = view 
    3822 
    39  
    40         def vend(self): # Vend Sucrose, and exit 
    41                 self.sucrose.vend(self.location,self.auth.uin) 
    42                 self.destruct() 
    43  
    44  
    45         def timeout(self): 
    46                 self.destruct() 
    47  
    48  
    49         def destruct(self): 
    50                 if hasattr(self, 'window'): 
    51                         self.window.destroy() 
    52                         del self.window 
     23                # I don't know why I need to do this, but I do 
     24                view.pack() 
     25                view.pack_forget() 
    5326 
    5427 
     
    5831 
    5932                self.window = Tkinter.Tk() 
     33         
     34                self.view_names = [] 
    6035 
    61                 # Add the objects 
    62                 self.objects=[] 
     36                self.views = Tkinter.Menu(self.window, font=("helvetica",24)) 
    6337 
    64                 self.objects.append(Tkinter.Button(self.window, text="0", command=self.make_button_callback(0), font=("Helvetica",72))) 
    65                 self.objects[0].grid(row=4,column=0,sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W) 
     38                self.current_view = None 
     39                self.make_view('Keypad', Keypad(sucrose,auth, exit=self.destroy)) 
     40                self.make_view('Details', Details(sucrose,auth, exit=self.destroy)) 
     41                self.make_view('Statistics', Statistics(sucrose,auth, exit=self.destroy)) 
     42 
     43 
     44                self.window.config(menu=self.views) 
     45 
     46                self.current_view.pack() 
     47 
     48                # Kill yourself after 15 seconds 
     49                self.window.after(15000,self.destroy) 
     50                self.views.add_command(label="Quit", command=self.destroy) 
     51                 
     52                self.window.mainloop() 
     53 
     54 
     55        def switch_view(self, view): 
     56                self.current_view.pack_forget() 
     57                self.current_view = view 
     58                view.pack() 
     59 
     60 
     61        def destroy(self): 
     62                if hasattr(self,'window'): 
     63                        for i in self.view_names: 
     64                                getattr(self,i).destroy() 
     65                        self.window.destroy() 
     66                        del self.window 
     67 
     68 
     69 
     70class Statistics(Tkinter.Frame): 
     71        def __init__(self, sucrose, auth, exit=None, location=None, master=None, cnf={}, **kw): 
     72                Tkinter.Frame.__init__(self,master=None,cnf={},**kw) 
     73 
     74                self.auth = auth  
     75                self.sucrose = sucrose 
     76                self.distruct = exit 
     77                self.location = location 
     78 
     79                # { BEGIN statistics GUI 
     80                self.Name = Tkinter.Label(self, text="Statistics", font=("helvetica", 72)) 
     81                self.Name.grid(row=0,column=0,sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W) 
     82                # } END statistics GUI 
     83 
     84 
     85 
     86class Details(Tkinter.Frame): 
     87        def __init__(self, sucrose, auth, exit=None, location=None, master=None, cnf={}, **kw): 
     88                Tkinter.Frame.__init__(self,master=None,cnf={},**kw) 
     89 
     90                self.auth = auth  
     91                self.sucrose = sucrose 
     92                self.distruct = exit 
     93                self.location = location 
     94 
     95                # { BEGIN details GUI 
     96                self.Name = Tkinter.Label(self, text="Details", font=("helvetica", 72)) 
     97                self.Name.grid(row=0,column=0,sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W) 
     98                # } END details GUI 
     99 
     100 
     101 
     102class Keypad(Tkinter.Frame): 
     103        def make_button_callback(self,number): 
     104                return lambda: self.button_callback(number) 
     105         
     106         
     107        def button_callback(self,number): 
     108                self.location_str=self.location_str[1]+str(number) 
     109                self.location.config(text=self.location_str) 
     110                self.name.config(text=sucrose.item_name_from_id(self.location_str)) 
     111                 
     112                # If it's not a valid purchase, don't allow the user to press the vend button 
     113                if self.auth.authenticate_purchase(self.location): 
     114                        self.vend.config(state=Tkinter.NORMAL) 
     115                else: 
     116                        self.vend.config(state=Tkinter.DISABLED) 
     117 
     118 
     119        def reset(self): 
     120                # If the location is 00, quit, else reset the display 
     121                if self.location_str == "00": 
     122                        self.distruct() 
     123                else: 
     124                        self.location_str="00" 
     125                        self.location.config(text=self.location_str) 
     126                        self.name.config(text="") 
     127                        self.vend.config(state=Tkinter.DISABLED) 
     128 
     129 
     130        def vend(self): # Vend Sucrose, and exit 
     131                self.sucrose.vend(self.location_str,self.auth.uin) 
     132                self.distruct() 
     133 
     134 
     135        def __init__(self, sucrose, auth, exit=None, location=None, master=None, cnf={}, **kw): 
     136                Tkinter.Frame.__init__(self,master=None,cnf={},**kw) 
     137 
     138                self.auth = auth  
     139                self.sucrose = sucrose 
     140                self.distruct = exit 
     141                self.location = location 
     142 
     143                # { BEGIN keypad GUI 
     144                Tkinter.Button(self, text="0", command=self.make_button_callback(0), \ 
     145                        font=("helvetica",72)).grid(row=4,column=0,sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W) 
    66146         
    67147                for y in range(0,4): 
     
    70150                                if i<10: 
    71151                                        callback=self.make_button_callback(i) 
    72                                         self.objects.append(Tkinter.Button(self.window, text=str(i), command=callback, font=("Helvetica", 72))) 
    73                                         self.objects[i].grid(row=y+1,column=x,sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W) 
     152                                        Tkinter.Button(self, text=str(i), command=callback, \ 
     153                                               font=("helvetica", 72)).grid(row=y+1,column=x,sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W) 
    74154 
    75155                # Special objects 
    76                 reset=Tkinter.Button(self.window, text="Reset", command=self.reset, font=("Helvetica", 24)) 
     156                reset=Tkinter.Button(self, text="Reset", command=self.reset, font=("helvetica", 24)) 
    77157                reset.grid(row=4,column=1,sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W) 
    78158 
    79                 self.vend=Tkinter.Button(self.window, text="Vend", command=self.vend, state=Tkinter.DISABLED, font=("Helvetica", 24)) 
     159                self.vend=Tkinter.Button(self, text="Vend", command=self.vend, state=Tkinter.DISABLED, font=("helvetica", 24)) 
    80160                self.vend.grid(row=4,column=2,sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W) 
    81161 
    82                 self.location="00" 
    83                 self.location_lbl=Tkinter.Label(self.window, text=self.location, font=("Helvetica", 72)) 
    84                 self.location_lbl.grid(row=0,column=0,sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W) 
     162                self.location_str="00" 
     163                self.location=Tkinter.Label(self, text=self.location_str, font=("helvetica", 72)) 
     164                self.location.grid(row=0,column=0,sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W) 
    85165                 
    86                 self.name_lbl=Tkinter.Label(self.window, text="", font=("Helvetica",24)) 
    87                 self.name_lbl.grid(row=0,column=1,columnspan=3,sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W) 
     166                self.name=Tkinter.Label(self, text="", font=("helvetica",24)) 
     167                self.name.grid(row=0,column=1,columnspan=3,sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W) 
    88168 
    89                 self.window.grid() 
    90                 self.window.mainloop() 
     169                self.grid() 
     170                # } END keypad GUI 
    91171 
    92172 
    93173if __name__ == "__main__": 
    94         k = Keypad(0,0) 
     174        TkinterUI(0,0) 
     175