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

2024-10-03 15:26:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,我想做一个购物车,我用的是w python,我从txt获取数据,我做了窗口和按钮事件,但是当我把数据附加到其他面板上时,这只在一个点上累积

inventario.txt文件

阿西恩托·曼特卡
植物园 查普林学院
塞西娜·恩奇拉达
查卢帕斯
查普林格兰德
西鲁埃拉·库蒂达
巧克力 巧克力\u bola \u pulida
巧克力蛋黄酱
巧克力蛋黄酱 巧克力\u rueda
巧克力小吃

窗口.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()

Tags: nameselfaddbuttonalllabelexpandwx
1条回答
网友
1楼 · 发布于 2024-10-03 15:26:39

您的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__defnot回调onbutton
此后,所有对p2sz的引用都应该是self.p2sz
实际上,每次单击按钮时都要声明p2sz

如果您还没有掌握self,您可能需要检查这里的解释:https://medium.com/quick-code/understanding-self-in-python-a3704319e5f0

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

相关问题 更多 >