如何将面板中的信息复制到另一个面板?

how can I copy the information in a panel to another panel?

我是 python 的新手,我想做一个购物车,我正在使用 w python,我从 txt 中获取数据,我已经完成了window 和按钮事件但是当我将数据附加到其他面板时,这只会累积一点

inventario.txt

asiento_manteca
cacahuate_botanero
cacahuate_chapulin
cecina_enchilada
查卢帕人
chapulin_grande
ciruela_curtida
chocolate_bola_azucarada chocolate_bola_pulida
chocolate_mayordomo
chocolate_mayordomo_烈酒 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()

inventario.txt 的数据不完整或结构不正确。
我假设您收到如下错误消息:

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 没有 label,它有 value
将该行更改为:

self.p2sz.Add(wx.SpinCtrl(p2,value=str(combo.GetSelection())),0,wx.EXPAND)

声明

self.p2sz = wx.GridSizer(len(productos),3,2,0)
p2.SetSizer(self.p2sz)

__init__ def not 在回调onbutton
此后所有对 p2sz 的引用都应指向 self.p2sz
事实上,每次单击按钮时,您都在声明 p2sz

如果您还没有掌握 self,您可能需要查看此处的说明:https://medium.com/quick-code/understanding-self-in-python-a3704319e5f0

附带说明一下,您的 wxPythonsizers 编码风格非常过时,我鼓励您放弃正在使用的任何资源并找到更现代的东西,例如https://wiki.wxpython.org/SizerTutorials