2
\$\begingroup\$

I developed a cost to ship app in python. To use this app, you will need to install wxpython.

Please review and tell me the areas i can improve.

import wx    # including a module

class Test(wx.Frame):    # creating a class
    def __init__(self, parent, id=wx.ID_ANY, title='McCastle App\'s - Shipping Calculator', pos=wx.DefaultPosition, size=wx.DefaultSize):
        super(Test, self).__init__(parent, id, title, pos, size=(600, 600))

        # creating Gui panel
        self.panel = wx.Panel(self)


        # 3 Text Entry boxes
        self.txt_input0 = wx.TextCtrl(self.panel, pos=(100, 20), size=(220, 22))
        self.txt_input1 = wx.TextCtrl(self.panel, pos=(100, 60), size=(330, 22))
        self.txt_input2 = wx.TextCtrl(self.panel, pos=(100, 100), size=(280, 22))


        # 3 Labels for Name, Address, and City State Zip
        self.lbl0 = wx.StaticText(self.panel, label="Name", pos=(330, 24))
        self.lbl1 = wx.StaticText(self.panel, label="Address", pos=(440, 66))
        self.lbl2 = wx.StaticText(self.panel, label="City, State, Zip Code", pos=(390, 100))


        # 1st GROUP Radio Labels for Weight, Speed, and Options 
        self.lbl3 = wx.StaticText(self.panel, label="Weight",pos=(30, 150))
        self.lbl4 = wx.StaticText(self.panel, label="Speed", pos=(180, 150))
        self.lbl5 = wx.StaticText(self.panel, label="Options", pos=(350, 150))


        # 2nd Group Radio Buttons Displayed on Panel
        self.Radio0 = wx.RadioButton(self.panel, label="0 - 1.9 lbs. $5",style = wx.RB_GROUP, pos=(20, 180))
        self.Radio1 = wx.RadioButton(self.panel, label="2 - 4.9 lbs. $8", pos=(20, 200))
        self.Radio2 = wx.RadioButton(self.panel, label="5 - 10 lbs. $12.25", pos=(20, 220))
        self.Radio3 = wx.RadioButton(self.panel, label="overland $2.75", style = wx.RB_GROUP, pos=(170, 180))
        self.Radio4 = wx.RadioButton(self.panel, label="3-day air $6.15", pos=(170, 200))
        self.Radio5 = wx.RadioButton(self.panel, label="2-day air $10.80", pos=(170, 220))
        self.Radio6 = wx.RadioButton(self.panel, label="overnight $15.25", pos=(170, 240))



        # Option Checkboxes
        self.cb0 = wx.CheckBox(self.panel, label="Extra padding. $5", pos=(350, 180))
        self.cb1 = wx.CheckBox(self.panel, label="Gift wrapping. $5", pos=(350, 200))


        # Buttons: Enter, Clear
        self.btn_enter = wx.Button(self.panel, label="Enter", pos=(160, 320))
        self.btn_clear = wx.Button(self.panel, label="Clear Form", pos=(300, 320))


        # Shipping Summary Label
        self.lbl5 = wx.StaticText(self.panel, label="Shipping Summary", pos=(220, 380))



        # Button Events Enter, Clear
        self.btn_enter.Bind(wx.EVT_BUTTON, self.myInfo)
        self.btn_clear.Bind(wx.EVT_BUTTON, self.clear_all)



        # Output Labels info from Name, Address, and State
        self.Line0 = wx.StaticText(self.panel, id = -1, pos=(100, 430), label="", )
        self.Line1 = wx.StaticText(self.panel, id = -1, pos=(100, 450), label="")
        self.Line2 = wx.StaticText(self.panel, id = -1, pos=(100, 470), label="")
        self.Line3 = wx.StaticText(self.panel, id = -1, pos=(100, 490), label="")





    # defining methods for form functionality
    def myInfo(self, event):
        cost = 0

        # creating conditions for Radio button 0 and 3
        if self.Radio0.GetValue() and self.Radio3.GetValue():
            cost = "$7.75"
            if self.cb0.GetValue():
                cost = "$12.75"
            if self.cb1.GetValue():
                cost = "$12.75"
            if self.cb0.GetValue() and self.cb1.GetValue():
                cost = "$17.75"


        # creating conditions for Radio button 0 and 4
        if self.Radio0.GetValue() and self.Radio4.GetValue():
            cost = "$11.15"
            if self.cb0.GetValue():
                cost = "$16.15"
            if self.cb1.GetValue():
                cost = "$16.15"
            if self.cb0.GetValue() and self.cb1.GetValue():
                cost = "$21.15"


        # creating conditions for Radio button 0 and 5
        if self.Radio0.GetValue() and self.Radio5.GetValue():
            cost = "$15.80"
            if self.cb0.GetValue():
                cost = "$20.80"
            if self.cb1.GetValue():
                cost = "$20.80"
            if self.cb0.GetValue() and self.cb1.GetValue():
                cost = "$25.80"



        # creating conditions for Radio button 0 and 6
        if self.Radio0.GetValue() and self.Radio6.GetValue():
            cost = "$20.25"
            if self.cb0.GetValue():
                cost = "$25.25"
            if self.cb1.GetValue():
                cost = "$25.25"
            if self.cb0.GetValue() and self.cb1.GetValue():
                cost = "$30.25"




        # creating conditions for Radio button 1 and 3
        if self.Radio1.GetValue() and self.Radio3.GetValue():
            cost = "$10.75"
            if self.cb0.GetValue():
                cost = "$15.75"
            if self.cb1.GetValue():
                cost = "$15.75"
            if self.cb0.GetValue() and self.cb1.GetValue():
                cost = "$20.75"

        # creating conditions for Radio button 1 and 4
        if self.Radio1.GetValue() and self.Radio4.GetValue():
            cost = "$14.15"
            if self.cb0.GetValue():
                cost = "$19.15"
            if self.cb1.GetValue():
                cost = "$19.15"
            if self.cb0.GetValue() and self.cb1.GetValue():
                cost = "$24.15"

        # creating conditions for Radio button 1 and 5
        if self.Radio1.GetValue() and self.Radio5.GetValue():
            cost = "$18.80"
            if self.cb0.GetValue():
                cost = "$23.80"
            if self.cb1.GetValue():
                cost = "$23.80"
            if self.cb0.GetValue() and self.cb1.GetValue():
                cost = "$28.80"

        # creating conditions for Radio button 1 and 6
        if self.Radio1.GetValue() and self.Radio6.GetValue():
            cost = "$23.25"
            if self.cb0.GetValue():
                cost = "$28.25"
            if self.cb1.GetValue():
                cost = "$28.25"
            if self.cb0.GetValue() and self.cb1.GetValue():
                cost = "$33.25"



        # creating conditions for Radio button 2 and 3
        if self.Radio2.GetValue() and self.Radio3.GetValue():
            cost = "$15.00"
            if self.cb0.GetValue():
                cost = "$18.00"
            if self.cb1.GetValue():
                cost = "$18.00"
            if self.cb0.GetValue() and self.cb1.GetValue():
                cost = "$23.00"

        # creating conditions for Radio button 2 and 4
        if self.Radio2.GetValue() and self.Radio4.GetValue():
            cost = "$18.40"
            if self.cb0.GetValue():
                cost = "$23.40"
            if self.cb1.GetValue():
                cost = "$23.40"
            if self.cb0.GetValue() and self.cb1.GetValue():
                cost = "$28.40"

        # creating conditions for Radio button 2 and 5
        if self.Radio2.GetValue() and self.Radio5.GetValue():
            cost = "$23.05"
            if self.cb0.GetValue():
                cost = "$28.05"
            if self.cb1.GetValue():
                cost = "$28.05"
            if self.cb0.GetValue() and self.cb1.GetValue():
                cost = "$33.05"

        # creating conditions for Radio button 2 and 6
        if self.Radio2.GetValue() and self.Radio6.GetValue():
            cost = "$27.50"
            if self.cb0.GetValue():
                cost = "$32.50"
            if self.cb1.GetValue():
                cost = "$32.50"
            if self.cb0.GetValue() and self.cb1.GetValue():
                cost = "$37.50"


        # creating object variables 

        data0 = self.txt_input0.GetValue()
        data1 = self.txt_input1.GetValue()
        data2 = self.txt_input2.GetValue()
        data3 =  cost

        # calling functions with objects
        self.Line0.SetLabel(data0)
        self.Line1.SetLabel(data1)
        self.Line2.SetLabel(data2)
        self.Line3.SetLabel(data3)




    # defining a method to clear data in form
    def clear_all(self, event):
        self.txt_input0.SetValue("")
        self.txt_input1.SetValue("")
        self.txt_input2.SetValue("")
        self.Radio0.SetValue(1)
        self.Radio1.SetValue(0)
        self.Radio2.SetValue(0)
        self.Radio3.SetValue(1)
        self.Radio4.SetValue(0)
        self.Radio5.SetValue(0)
        self.Radio6.SetValue(0)
        self.cb0.SetValue(0)
        self.cb1.SetValue(0)
        clsData = ""
        self.Line0.SetLabel(clsData)
        self.Line1.SetLabel(clsData)
        self.Line2.SetLabel(clsData)
        self.Line3.SetLabel(clsData)



if __name__ == "__main__":

    app = wx.App()
    fr = Test(None, -1)
    fr.Show()
    app.MainLoop()
\$\endgroup\$
  • \$\begingroup\$ I'm sure I could condense the if conditionals, but I needed to get the app working first. \$\endgroup\$ – Jerime McCastle Oct 1 '16 at 15:52
  • 1
    \$\begingroup\$ Using names like Radio3 is worse (less readable) than overland_option and also worse than radio[3] - does not allow processing in bulk. Either use readable names or use arrays IMO. \$\endgroup\$ – Daerdemandt Oct 1 '16 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.