i'm new on python, I want to do a do a shoping cart and I'm using w python, I take the data from txt, and I've done the window and the button events but when I append the data to other panel, this accumulate only in one point
inventario.txt
asiento_manteca
cacahuate_botanero
cacahuate_chapulin
cecina_enchilada
chalupas
chapulin_grande
ciruela_curtida
chocolate_bola_azucarada
chocolate_bola_pulida
chocolate_mayordomo
chocolate_mayordomo_licor
chocolate_rueda
chocolate_tablilla
window.py
import wx
import wx.lib.scrolledpanel
productos=[]
with open('inventario.txt') as lineas:
for linea in lineas:
texto=linea.split()
productos.append(texto)
print(texto)
class MiAplicacion(wx.Frame):
def onButton(self, event):
button = event.GetEventObject()
spiner=self.FindWindowByName('s'+button.GetName())
s=spiner.GetValue()
combo=self.FindWindowByName('c'+button.GetName())
print(combo.GetValue())
print(s)
p2=self.FindWindowByName('panel')
p2sz = wx.GridSizer(len(productos),3,2,0)
p2sz.Add(wx.StaticText(p2,label=productos[int(button.GetName())][0]),0,wx.ALL)
p2sz.Add(wx.StaticText(p2,label=str(s)),0,wx.EXPAND)
p2sz.Add(wx.SpinCtrl(p2,label=combo.GetValue()),0,wx.EXPAND)
p2.SetSizer(p2sz)
def __init__ (self,parent,title):
screenSize = wx.DisplaySize()
screenWidth = screenSize[0]
screenHeight = screenSize[1]
wx.Frame.__init__(self,parent=parent,title=title,size=(screenWidth,screenHeight))
p1 = wx.lib.scrolledpanel.ScrolledPanel(self,-1,pos=(0,0),size=(screenWidth/2,screenHeight))
p2 = wx.lib.scrolledpanel.ScrolledPanel(self,-1,pos=(0,screenWidth/2),size=(screenWidth/2,screenHeight/2),name="panel")
p1.SetBackgroundColour('yellow')
p2.SetBackgroundColour('blue')
p1.SetupScrolling()
p2.SetupScrolling()
mainsz = wx.BoxSizer(wx.HORIZONTAL)
p1sz = wx.GridSizer(len(productos),4,2,0)
for i in range (0,len(productos)):
p1sz.Add(wx.StaticText(p1,label=productos[i][0]),0,wx.ALL)
choi=productos[i][1].split('/')
p1sz.Add(wx.ComboBox(p1,value=choi[0],size=(0,0),choices=choi,name='c'+str(i)),0,wx.EXPAND)
p1sz.Add(wx.SpinCtrl(p1,initial=0,max=10000,name='s'+str(i)),0,wx.EXPAND)
b=wx.Button(p1,label = "agregar",name=str(i))
b.Bind(wx.EVT_BUTTON, self.onButton)
p1sz.Add(b)
mainsz.Add(p1,1,wx.ALL,10)
mainsz.Add(p2,1,wx.ALL,10)
p1.SetSizer(p1sz)
self.SetSizer(mainsz)
self.Centre(True)
self.Show()
if __name__=='__main__':
app=wx.App()
frame=MiAplicacion(None,u"Oaxaca")
app.MainLoop()
Your data for inventario.txt
is incomplete or not structured properly.
I assume that you are getting an error message like the following:
Traceback (most recent call last):
File "window.py", line 22, in onButton
p2sz.Add(wx.SpinCtrl(p2,label=combo.GetValue()),0,wx.EXPAND)
TypeError: SpinCtrl(): arguments did not match any overloaded call:
overload 1: too many arguments
overload 2: 'label' is not a valid keyword argument
wx.SpinCtrl
does not have a label
, it does have a value
.
change that line to:
self.p2sz.Add(wx.SpinCtrl(p2,value=str(combo.GetSelection())),0,wx.EXPAND)
Declare
self.p2sz = wx.GridSizer(len(productos),3,2,0)
p2.SetSizer(self.p2sz)
in the __init__
def not in the callback onbutton
thereafter all references to p2sz should be to self.p2sz
As it is, you are declaring p2sz
each and every time you click a button.
If you haven't got to grips with self
, you might want to check the explanation here: https://medium.com/quick-code/understanding-self-in-python-a3704319e5f0
As a side note, your style of coding for wxPython
sizers is extremely old fashioned and I'd encourage you to discard whatever resource you are using and find something more modern e.g. https://wiki.wxpython.org/SizerTutorials