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()