#!/usr/bin/python import sys import Tkinter class divvy_win: def __init__( self ): #-- Create the root window self.root = Tkinter.Tk() self.root.title( 'Who\'s Paying?' ) #-- Create the variables referenced later self.contributors = {} self.calc_text = Tkinter.StringVar() self.item_text = Tkinter.StringVar() #-- When we start packing grocery items, this will be the row self.current_item = 2 #-- Create a frame for the item input calc_frame = Tkinter.Frame( self.root ) calc_frame.grid( row=0, columnspan=len(sys.argv) ) #-- Pack in text entry widgets and the 'Add' button Tkinter.Label( calc_frame, text = 'Item: ' ).pack( side = Tkinter.LEFT ) Tkinter.Entry( calc_frame, width = 10, textvariable = self.item_text ).pack( side = Tkinter.LEFT ) Tkinter.Label( calc_frame, text = 'Price: ' ).pack( side = Tkinter.LEFT ) Tkinter.Entry( calc_frame, width = 10, textvariable = self.calc_text ).pack( side = Tkinter.LEFT ) Tkinter.Button( calc_frame, text = 'Add', command=self.update_columns ).pack( side = Tkinter.LEFT ) def add_contributor( self, name ): #-- Add keys to all of the dictionaries self.contributors[ name ] = [ len( self.contributors ), # Position / column 0, # Total contribution Tkinter.IntVar(), # Value of checkbox Tkinter.StringVar() ] # Label displaying total #-- Update the values of the labels self.contributors[ name ][3].set( ' %s:%d.%.2d ' % ( name, self.contributors[ name ][1] / 100, self.contributors[ name ][1] % 100 ) ) #-- Create a frame for the label and checkbox nameframe = Tkinter.Frame( self.root ) nameframe.grid( row=1, column=self.contributors[name][0] ) self.root.columnconfigure( self.contributors[name][0], weight=1 ) #-- Add the label and checkbox to the frame Tkinter.Checkbutton( nameframe, variable = self.contributors[name][2] ).pack( side = Tkinter.LEFT ) Tkinter.Label( nameframe, textvariable = self.contributors[name][3] ).pack( side = Tkinter.LEFT ) def update_columns( self ): numval = 0 vectors = 0 # Figure out the total value for this item if( self.calc_text.get() != '' ): numval = eval( self.calc_text.get() ) * 100 # Count the people paying for this item for x in self.contributors.keys(): if( self.contributors[ x ][2].get() ): vectors = vectors + 1 if( vectors == 0 ): return numval = int( numval / vectors ) for name in self.contributors.keys(): if( self.contributors[ name ][2].get() ): self.contributors[ name ][1] = self.contributors[ name ][1] + numval self.contributors[ name ][3].set( ' %s:%d.%.2d ' % ( name, self.contributors[ name ][1] / 100, self.contributors[ name ][1] % 100 ) ) Tkinter.Label( self.root, text = '%s : %d.%.2d' % ( self.item_text.get(), numval / 100, numval % 100 ) ).grid( row=self.current_item, column=self.contributors[name][0], sticky=Tkinter.E ) else: Tkinter.Label( self.root, text = '%s : 0.00' % ( self.item_text.get() ) ).grid( row=self.current_item, column=self.contributors[name][0], sticky=Tkinter.E ) self.current_item = self.current_item + 1 self.calc_text.set( '' ) self.item_text.set( '' ) def run( self ): self.root.mainloop() ############################## ## main() ############################## app = divvy_win() for person in range( 1, len(sys.argv) ): app.add_contributor( sys.argv[ person ] ) app.run()