im trying to make it so that the discount box will only accept numeric input to 2dp, and i know the code is roughly solid, but when i run the code, it says CashTransaction has no attribute discount input, and i have been tinkering with this code for the better part of 3 hours now.

i know this code can be run if i put self. in front of pretty much everything, but there must be a simpler way that i am missing, can anyone please help?

import wx

class CashTransaction(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, title = "Cash Transaction",
                                                  size = (450, 470))

            mainpanel = wx.Panel(self)

            font = wx.Font(20, wx.SWISS, wx.NORMAL, wx.NORMAL, False, "Mangal")
            title = wx.StaticText(mainpanel, label = "Cash Transaction", style = wx.ALIGN_CENTRE)
            title.SetFont(font)

            titleholder = wx.BoxSizer()
            titleholder.Add(title, 1 , wx.EXPAND)
            mainpanel.SetSizer(titleholder)

            calcpanel = wx.Panel(self)


            self.btn_list = [
                
            "7", "8", "9",
            "4", "5", "6",
            "1", "2", "3",
            "0", ".", "BackSpace"
                            ]

            gsizer = wx.GridSizer(4, 3, 0, 0)

            self.btn = range(len(self.btn_list))
            for ix, b_label in enumerate(self.btn_list):
            
                id = 10 + ix
                self.btn[ix] = wx.Button(calcpanel, id, label=b_label, size=(-1, -1))
                
                gsizer.Add(self.btn[ix], 0, wx.ALL|wx.EXPAND)
                calcpanel.SetSizer(gsizer)


            infoandinputpanel = wx.Panel(self, style = wx.SUNKEN_BORDER)


            totalamount = 0.00


            font2 = wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL, False, "Mangal")

            totallabel = wx.StaticText(infoandinputpanel,
                            label = "Transaction Total\n""%s" %(totalamount))
            totallabel.SetFont(font2)

            discountlabel = wx.StaticText(infoandinputpanel, label = "Discount\n")
            discountlabel.SetFont(font2)
            discountinput = wx.TextCtrl(infoandinputpanel)

            discountinput.Bind(wx.EVT_TEXT_ENTER, self.onAction)

            discspacer1 = wx.BoxSizer()
            discspacer2 = wx.BoxSizer()


            discsizer = wx.BoxSizer(wx.VERTICAL)
            discsizer.Add(discountlabel, 1)
            discsizer.Add(discspacer1, 1)
            discsizer.Add(discountinput, 1)
            discsizer.Add(discspacer2, 1)

            paidlabel = wx.StaticText(infoandinputpanel,
                                      label = "Amount Recieved")
            paidlabel.SetFont(font2)
            
            paidinput = wx.TextCtrl(infoandinputpanel)          
            paidspacer1 = wx.BoxSizer()
            paidspacer2 = wx.BoxSizer()

            paidsizer = wx.BoxSizer(wx.VERTICAL)
            paidsizer.Add(paidlabel, 1)
            paidsizer.Add(paidspacer1, 1)
            paidsizer.Add(paidinput, 1)
            paidsizer.Add(paidspacer2, 1)

            changefigure = 1.29

            changelabel = wx.StaticText(infoandinputpanel, label = ("Change to give\n""%s" % (changefigure)))
            changelabel.SetFont(font2)
            

            iandisizer = wx.BoxSizer(wx.VERTICAL)
            iandisizer.Add(totallabel, 1)
            iandisizer.Add(discsizer, 1, wx.EXPAND)
            iandisizer.Add(paidsizer, 1, wx.EXPAND)
            iandisizer.Add(changelabel, 1, wx.EXPAND)

            iandispacer = wx.wx.BoxSizer()


            iandilayout = wx.BoxSizer()
            iandilayout.Add(iandispacer, 1)
            iandilayout.Add(iandisizer, 9, wx.EXPAND)


            infoandinputpanel.SetSizer(iandilayout)

            lowersizer = wx.BoxSizer()
            lowersizer.Add(calcpanel, 3, wx.EXPAND)
            lowersizer.Add(infoandinputpanel, 2, wx.EXPAND)

            mainsizerleft = wx.BoxSizer(wx.VERTICAL)
            mainsizerleft.Add(mainpanel, 1, wx.EXPAND)
            mainsizerleft.Add(lowersizer, 4, wx.EXPAND)

              
            self.SetSizer(mainsizerleft)


        def onAction(self, event):

            disc_value = self.discountinput.GetValue().strip()

            if all(x in "0123456789.+-" for x in disc_value):

                value = round(float(disc_value), 2)
            else:
                discountinput.ChangeValue("number only")         


app = wx.App(redirect=False)
window = CashTransaction()
window.Show()
app.MainLoop()

Recommended Answers

All 7 Replies

Has nothing to do with putting 'self.' on front of everything, but at least being coinsistent on the variables. Look here:

def onAction(self, event):
    disc_value = self.discountinput.GetValue().strip()
    if all(x in "0123456789.+-" for x in disc_value):
        value = round(float(disc_value), 2)
    else:
        discountinput.ChangeValue("number only")

Is it discountinput, or the discountinput from the class (self.discountinput)?

When you declare a class yes define attributes to the class by wirting them as 'self.'.

If you don't declare them as attributes you may not call them as attributes.

Cheers and Happy coding

Tried it that way, it says Global variable discoutinput is not declared. ive tinkered with this code for hours now, and looked on the internet for the solutions, but i cant seem to get it right.

thanks for your input though, i did learn something.

import wx

class CashTransaction(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, title = "Cash Transaction",
                                                  size = (450, 470))

            mainpanel = wx.Panel(self)

            font = wx.Font(20, wx.SWISS, wx.NORMAL, wx.NORMAL, False, "Mangal")
            title = wx.StaticText(mainpanel, label = "Cash Transaction", style = wx.ALIGN_CENTRE)
            title.SetFont(font)

            titleholder = wx.BoxSizer()
            titleholder.Add(title, 1 , wx.EXPAND)
            mainpanel.SetSizer(titleholder)

            calcpanel = wx.Panel(self)


            self.btn_list = [
                
            "7", "8", "9",
            "4", "5", "6",
            "1", "2", "3",
            "0", ".", "BackSpace"
                            ]

            gsizer = wx.GridSizer(4, 3, 0, 0)

            self.btn = range(len(self.btn_list))
            for ix, b_label in enumerate(self.btn_list):
            
                id = 10 + ix
                self.btn[ix] = wx.Button(calcpanel, id, label=b_label, size=(-1, -1))
                
                gsizer.Add(self.btn[ix], 0, wx.ALL|wx.EXPAND)
                calcpanel.SetSizer(gsizer)


            infoandinputpanel = wx.Panel(self, style = wx.SUNKEN_BORDER)


            totalamount = 0.00


            font2 = wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL, False, "Mangal")

            totallabel = wx.StaticText(infoandinputpanel,
                            label = "Transaction Total\n""%s" %(totalamount))
            totallabel.SetFont(font2)

            discountlabel = wx.StaticText(infoandinputpanel, label = "Discount\n")
            discountlabel.SetFont(font2)
            discountinput = wx.TextCtrl(infoandinputpanel)

            discountinput.Bind(wx.EVT_TEXT_ENTER, self.onAction)

            discspacer1 = wx.BoxSizer()
            discspacer2 = wx.BoxSizer()


            discsizer = wx.BoxSizer(wx.VERTICAL)
            discsizer.Add(discountlabel, 1)
            discsizer.Add(discspacer1, 1)
            discsizer.Add(discountinput, 1)
            discsizer.Add(discspacer2, 1)

            paidlabel = wx.StaticText(infoandinputpanel,
                                      label = "Amount Recieved")
            paidlabel.SetFont(font2)
            
            paidinput = wx.TextCtrl(infoandinputpanel)          
            paidspacer1 = wx.BoxSizer()
            paidspacer2 = wx.BoxSizer()

            paidsizer = wx.BoxSizer(wx.VERTICAL)
            paidsizer.Add(paidlabel, 1)
            paidsizer.Add(paidspacer1, 1)
            paidsizer.Add(paidinput, 1)
            paidsizer.Add(paidspacer2, 1)

            changefigure = 1.29

            changelabel = wx.StaticText(infoandinputpanel, label = ("Change to give\n""%s" % (changefigure)))
            changelabel.SetFont(font2)
            

            iandisizer = wx.BoxSizer(wx.VERTICAL)
            iandisizer.Add(totallabel, 1)
            iandisizer.Add(discsizer, 1, wx.EXPAND)
            iandisizer.Add(paidsizer, 1, wx.EXPAND)
            iandisizer.Add(changelabel, 1, wx.EXPAND)

            iandispacer = wx.wx.BoxSizer()


            iandilayout = wx.BoxSizer()
            iandilayout.Add(iandispacer, 1)
            iandilayout.Add(iandisizer, 9, wx.EXPAND)


            infoandinputpanel.SetSizer(iandilayout)

            lowersizer = wx.BoxSizer()
            lowersizer.Add(calcpanel, 3, wx.EXPAND)
            lowersizer.Add(infoandinputpanel, 2, wx.EXPAND)

            mainsizerleft = wx.BoxSizer(wx.VERTICAL)
            mainsizerleft.Add(mainpanel, 1, wx.EXPAND)
            mainsizerleft.Add(lowersizer, 4, wx.EXPAND)

              
            self.SetSizer(mainsizerleft)


        def onAction(self, event):

            disc_value = discountinput.GetValue().strip()

            if all(x in "0123456789.+-" for x in disc_value):

                value = round(float(disc_value), 2)
            else:
                discountinput.ChangeValue("number only")         


app = wx.App(redirect=False)
window = CashTransaction()
window.Show()
app.MainLoop()

I did nothing to the code mate, I just showed you a part of your code where within 5 lines you call two different things to the 'same' variable.

Cheers and Happy coding

how do i make the .GetValue look for the wx.TextCtrl as opposed to searching through global variables?

self has an important role in a class,it like a transporter off data in a class.
You use self.discountinput in onAction() method.
Then off course it will not find anything if you not using self on what it`s looking for.
Here is a fix so wx.TextCtrl return data in to method onAction()

import wx

class CashTransaction(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, title = "Cash Transaction",
                                                  size = (450, 470))

            mainpanel = wx.Panel(self)

            font = wx.Font(20, wx.SWISS, wx.NORMAL, wx.NORMAL, False, "Mangal")
            title = wx.StaticText(mainpanel, label = "Cash Transaction", style = wx.ALIGN_CENTRE)
            title.SetFont(font)

            titleholder = wx.BoxSizer()
            titleholder.Add(title, 1 , wx.EXPAND)
            mainpanel.SetSizer(titleholder)

            calcpanel = wx.Panel(self)


            self.btn_list = [
                
            "7", "8", "9",
            "4", "5", "6",
            "1", "2", "3",
            "0", ".", "BackSpace"
                            ]

            gsizer = wx.GridSizer(4, 3, 0, 0)

            self.btn = range(len(self.btn_list))
            for ix, b_label in enumerate(self.btn_list):
            
                id = 10 + ix
                self.btn[ix] = wx.Button(calcpanel, id, label=b_label, size=(-1, -1))
                
                gsizer.Add(self.btn[ix], 0, wx.ALL|wx.EXPAND)
                calcpanel.SetSizer(gsizer)


            infoandinputpanel = wx.Panel(self, style = wx.SUNKEN_BORDER)


            totalamount = 0.00


            font2 = wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL, False, "Mangal")

            totallabel = wx.StaticText(infoandinputpanel,
                            label = "Transaction Total\n""%s" %(totalamount))
            totallabel.SetFont(font2)

            discountlabel = wx.StaticText(infoandinputpanel, label = "Discount\n")
            discountlabel.SetFont(font2)
            
            
            self.discountinput = wx.TextCtrl(infoandinputpanel)  #**
            self.discountinput.Bind(wx.EVT_TEXT_ENTER, self.onAction) #**


            discspacer1 = wx.BoxSizer()
            discspacer2 = wx.BoxSizer()

            discsizer = wx.BoxSizer(wx.VERTICAL)
            discsizer.Add(discountlabel, 1)
            discsizer.Add(discspacer1, 1)
            discsizer.Add(self.discountinput, 1)  #**
            discsizer.Add(discspacer2, 1)

            paidlabel = wx.StaticText(infoandinputpanel,
                                      label = "Amount Recieved")
            paidlabel.SetFont(font2)
            
            paidinput = wx.TextCtrl(infoandinputpanel)          
            paidspacer1 = wx.BoxSizer()
            paidspacer2 = wx.BoxSizer()

            paidsizer = wx.BoxSizer(wx.VERTICAL)
            paidsizer.Add(paidlabel, 1)
            paidsizer.Add(paidspacer1, 1)
            paidsizer.Add(paidinput, 1)
            paidsizer.Add(paidspacer2, 1)

            changefigure = 1.29

            changelabel = wx.StaticText(infoandinputpanel, label = ("Change to give\n""%s" % (changefigure)))
            changelabel.SetFont(font2)
            

            iandisizer = wx.BoxSizer(wx.VERTICAL)
            iandisizer.Add(totallabel, 1)
            iandisizer.Add(discsizer, 1, wx.EXPAND)
            iandisizer.Add(paidsizer, 1, wx.EXPAND)
            iandisizer.Add(changelabel, 1, wx.EXPAND)

            iandispacer = wx.wx.BoxSizer()


            iandilayout = wx.BoxSizer()
            iandilayout.Add(iandispacer, 1)
            iandilayout.Add(iandisizer, 9, wx.EXPAND)


            infoandinputpanel.SetSizer(iandilayout)

            lowersizer = wx.BoxSizer()
            lowersizer.Add(calcpanel, 3, wx.EXPAND)
            lowersizer.Add(infoandinputpanel, 2, wx.EXPAND)

            mainsizerleft = wx.BoxSizer(wx.VERTICAL)
            mainsizerleft.Add(mainpanel, 1, wx.EXPAND)
            mainsizerleft.Add(lowersizer, 4, wx.EXPAND)
              
            self.SetSizer(mainsizerleft)

        def onAction(self, event):
            #You are calling discountinput with self **
            disc_value = self.discountinput.GetValue().strip()
            print disc_value #Now you see that GetValue() works correct

            if all(x in "0123456789.+-" for x in disc_value):

                value = round(float(disc_value), 2)
            else:

wow, i did half of the things that you did, just not at the same time, so now i feel like a complete tool.

Thanks though, seeing the complete picture makes it make alot more sense to me, especially with the annotations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.