[Tutor] First Gui App - Please scrutinize

Paul Kraus pkraus at pelsupply.com
Mon Feb 6 21:34:34 CET 2006


This is my first gui application. It just takes a percentage and then based on 
some options calculates a new price.

-----------------------------------
#!/usr/bin/python
from Tkinter import *

class Application( Frame ):
    """ Price Calc GUI """
    def __init__( self, master):
        Frame.__init__( self, master )
        self.grid()
        self.costprice = 0.0
        self.listprice = 0.0
        self.margmarkup = 0.0
        self.formula = ''
        self.mmradial = IntVar()
        self.costlistradial = IntVar()
        self.create_widgets()

    def create_widgets( self ):
        self.marginormarkup()
        self.entryboxes()
        self.costlist()
        
        # Calculate Button
        self.b_calc = Button( self )
        self.b_calc[ 'text'    ] = 'Calculate'
        self.b_calc[ 'command' ] = self.calculate
        self.b_calc.grid(row=10,column=0)
    
    def marginormarkup(self):
        # Calc using margin or markup
        Label( self, text = 'Margin or 
Markup...').grid(row=0,column=0,sticky=W)
        Radiobutton( self, text = 'Margin',  variable = self.mmradial, command 
= self.setstatusmm, value = 1).grid(row=1,column=0,sticky=W)
        Radiobutton( self, text = 'Mark-Up', variable = self.mmradial, command 
= self.setstatusmm, value = 0).grid(row=2,column=0,sticky=W)
        self.e_marginmarkup = Entry( self )
        self.e_marginmarkup.grid(row=3,column=0,sticky=W)
        Label( self, text = 'Percentage').grid(row=3,column=1,sticky=W)
        self.mmradial.set(1)

    def costlist(self):
        # From Radial
        Label( self, text = 'Calculations Based 
On...').grid(row=4,column=0,sticky=NW)
        self.test = Radiobutton( self, text = 'Cost', variable = 
self.costlistradial, value = 1, command = 
self.setstatus).grid(row=5,column=0,sticky=W)
        self.test = Radiobutton( self, text = 'List', variable = 
self.costlistradial, value = 0, command = 
self.setstatus).grid(row=6,column=0,sticky=W)
        self.costlistradial.set(1)
        self.setstatus()

    def entryboxes(self):
         # Entry Boxes Cost List Result
        self.e_cost = Entry( self )
        self.e_list = Entry( self )

        Label(self, text='Cost').grid(row=7,column=1,sticky=W)
        self.e_cost.grid(row=7,column=0,sticky=W)

        Label(self, text='List').grid(row=8,column=1,sticky=W)
        self.e_list.grid(row=8,column=0,sticky=W)

    def setstatusmm( self ):
        if ( not self.costlistradial.get()):
            self.mmradial.set(0)

    def setstatus( self ):
        self.delentryboxes()
        if (self.costlistradial.get()):
            self.e_list['state'] = 'disabled'
            self.e_cost['state'] = 'normal'
        else:
            self.e_cost['state'] = 'disabled'
            self.e_list['state'] = 'normal'
            self.mmradial.set(0)
        self.e_list.delete(0,END)
        self.e_cost.delete(0,END)
        self.e_marginmarkup.delete(0,END)

    def delentryboxes( self ):
        boxes = [ self.e_list, self.e_cost, self.e_marginmarkup ]
        for ebox in boxes:
            ebox['state'] = 'normal'
            ebox.delete(0,END)
            

    def calculate( self ):
        equation = None
        target = None
        costa = float(self.e_cost.get())
        lista = float(self.e_lista.get())
        mma   = float(self.e_marginmarkup.get())/100
        if (self.mmradial.get()): ### If Margin
            if ( self.costlistradial.get() ):
                equation = costa / ( 1 - mma)
                target = self.e_list
        else: ### If Markup
            if ( self.costlistradial.get() ):
                equation = cost + ( cost * mma )
                target = self.e_list
            else:
                equation = list - (list * mma)
                target = self.e_cost
        target['state'] = 'normal'
        target.delete(0,END)   
        target.insert(0,equation)
        target['state'] = 'readonly'


root = Tk()
root.title('Price Calculations')
root.geometry( '200x225' )
pricecalcgui = Application(root)
root.mainloop()


More information about the Tutor mailing list