0

Estoy creando una ventana sencilla donde el usuario va a ingresar un numero n de valores, es decir, el usuario decide cuantos valores va a ingresar. Quiero que los ingrese en un wx.TextCtrl y agregar en tiempo real en el evento del botón ok. (la función del evento del botón se llama iniciarCacillas() ) los wx.textCtrl a un sizer donde organizo todo.

El problema es que al momento de hacerlo y crear los TextCtrl de esta forma para luego agregarlos al sizer no funciona, se quedan los TextCtrl en la esquina del frame, uno sobre otro.

Este es mi código:

import wx


class ventana(wx.Frame):

    def __init__(self):
        app = wx.App()
        wx.Frame.__init__(self, None, title = "Ajute De Curvas", size = (500, 700))
        #paneles

        self.SetBackgroundColour(wx.WHITE)
        #sizer

        self.sUno = wx.BoxSizer(wx.VERTICAL)
        self.sDos = wx.BoxSizer(wx.HORIZONTAL)
        self.sBox = wx.StaticBoxSizer(wx.VERTICAL, self, "Introduzca el valor de los puntos")
        self.sTres = wx.BoxSizer(wx.HORIZONTAL)
        self.sCuatro = wx.BoxSizer(wx.HORIZONTAL)
        self.sCinco = wx.BoxSizer(wx.HORIZONTAL)

        #butons

        self.bCalcular = wx.Button(self, -1, "Calcular")
        self.bBorrar = wx.Button(self, -1, "Borrar")
        self.bOk = wx.Button(self, -1, "Ok", size = (30,28))

        #labels
        self.lUno = wx.StaticText(self, -1, "numero de Puntos (n) : ")
        self.lDos = wx.StaticText(self, -1, "Introduzca El Valor De Los Puntos")
        self.lTres = wx.StaticText(self, -1, "Valores De X ")
        self.lCuatro = wx.StaticText(self, -1, "Valores De Y ")
        self.lCinco = wx.StaticText(self, -1, "Lineal")
        self.lSeis = wx.StaticText(self, -1, " ".ljust(20 , " ") + "r = ")
        self.lSiete = wx.StaticText(self, -1, "Cuadratico")
        self.lOcho = wx.StaticText(self, -1, " ".ljust(20 , " ") + "r = ")
        self.lNueve = wx.StaticText(self, -1, "Grado 3")
        self.lDiez = wx.StaticText(self, -1, " ".ljust(20 , " ") + "r = ")
        self.lOnce = wx.StaticText(self, -1, "Grado 4")
        self.lDoce = wx.StaticText(self, -1, " ".ljust(20 , " ") + "r = ")
        self.lTrece = wx.StaticText(self, -1, "Grado 5")
        self.lCatorce = wx.StaticText(self, -1, " ".ljust(20 , " ") + "r = ")

        #textControl
        self.tUno = wx.TextCtrl(self, size = (50, 25))
        self.listaT = [[],[]]

        # Static line
        self.lineOne = wx.StaticLine(self, -1, size = (430, 1), style = (wx.LI_HORIZONTAL))
        self.lineTwo = wx.StaticLine(self, -1, size =(430, 1), style = (wx.LI_HORIZONTAL) )

        #organizando sizers
        self.sDos.Add(self.lUno, wx.SizerFlags().Centre().Border(wx.ALL, 10))
        self.sDos.Add(self.tUno,  wx.SizerFlags().Centre().Border(wx.ALL, 13))
        self.sDos.Add(self.bOk,  wx.SizerFlags().Centre().Border(wx.ALL, 13))
        self.sUno.Add(self.sDos, 1, wx.EXPAND)

        self.sUno.Add(self.lDos,wx.SizerFlags().Left().Border(wx.ALL, 13) )
        self.sUno.Add(self.lineOne,wx.SizerFlags().Centre().Border(wx.ALL, 13))

        self.sTres.Add(self.lTres,wx.SizerFlags().Left().Border(wx.ALL, 13) )
        self.sBox.Add(self.sTres, 1 , wx.Left)

        self.sCuatro.Add(self.lCuatro, wx.SizerFlags().Left().Border(wx.ALL, 13))
        self.sBox.Add(self.sCuatro, 1 , wx.Left)

        self.sUno.Add(self.sBox, 2, wx.EXPAND | wx.ALL, 13)
        self.sUno.Add(self.lineTwo, wx.SizerFlags().Centre().Border(wx.ALL, 13))

        self.sCinco.Add(self.bCalcular, wx.ALIGN_CENTER_HORIZONTAL)
        self.sCinco.Add(self.bBorrar, wx.ALIGN_CENTER_HORIZONTAL )
        self.sUno.Add(self.sCinco, 1 , wx.ALIGN_CENTER_HORIZONTAL)

        self.sUno.Add(self.lCinco, wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sUno.Add(self.lSeis, wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sUno.Add(self.lSiete, wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sUno.Add(self.lOcho, wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sUno.Add(self.lNueve , wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sUno.Add(self.lDiez , wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sUno.Add(self.lOnce , wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sUno.Add(self.lDoce , wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sUno.Add(self.lTrece , wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sUno.Add(self.lCatorce , wx.SizerFlags().Left().Border(wx.ALL, 9))

        #eventos
        self.Bind(wx.EVT_BUTTON, self.iniciarCacillas, self.bOk)

        #acabados de la ventana
        self.SetSizer(self.sUno)
        self.Centre()
        self.Show()
        app.MainLoop()

    def iniciarCacillas(self, evt):
        #try:
        self.n = int(self.tUno.GetValue())

        for i in range(self.n):
            cUno = wx.TextCtrl(self)
            cDos = wx.TextCtrl(self)

            self.sTres.Add(cUno, 1, wx.EXPAND | wx.ALL , 10)
            self.sCuatro.Add(cDos, 1, wx.EXPAND | wx.ALL , 10)

            self.listaT[0].append(cUno)
            self.listaT[1].append(cDos)
        #except:
            #wx.MessageDialog(None, "Digite numeros validos", "SeeD.Inc Msj", wx.OK).ShowModal()


v = ventana()

1 respuesta 1

0

Si después de agregar los widgets, redimensionas la ventana verás como se recolocan correctamente.

El problema es que debes llamar explícitamente al método Layout de algún widget padre, el cual es llamado cuando se reciben eventosEVT_SIZE (como redimensionar la ventana, de ahí el comportamiento anterior) si no hemos asignado nuestro propio handler. Esta llamada no se produce en este caso con simplemente añadir el widget al Sizer.

Puedes hacer self.sBox.Layout o self.Layout, dado que el evento se va a propagar, en el método mediante el que agregas los TextCtrl.

El código, con algunas modificaciones, podría quedar:

import wx


class Ventana(wx.Frame):

    def __init__(self, title):        
        super().__init__(None, title=title, size=(500, 700))

        # paneles
        self.SetBackgroundColour(wx.WHITE)

        # sizer
        self.sizer_uno = wx.BoxSizer(wx.VERTICAL)
        self.sizer_dos = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer_box = wx.StaticBoxSizer(wx.VERTICAL, self, "Introduzca el valor de los puntos")
        self.sizer_tres = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer_cuatro = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer_cinco = wx.BoxSizer(wx.HORIZONTAL)

        # botones
        self.btn_calcular = wx.Button(self, -1, "Calcular")
        self.btn_borrar = wx.Button(self, -1, "Borrar")
        self.btn_ok = wx.Button(self, -1, "Ok", size=(30,28))

        # labels
        self.lbl_uno = wx.StaticText(self, -1, "numero de Puntos (n) : ")
        self.lbl_dos = wx.StaticText(self, -1, "Introduzca El Valor De Los Puntos")
        self.lbl_tres = wx.StaticText(self, -1, "Valores De X ")
        self.lbl_cuatro = wx.StaticText(self, -1, "Valores De Y ")
        self.lbl_cinco = wx.StaticText(self, -1, "Lineal")
        self.lbl_seis = wx.StaticText(self, -1, " ".ljust(20 , " ") + "r = ")
        self.lbl_siete = wx.StaticText(self, -1, "Cuadratico")
        self.lbl_ocho = wx.StaticText(self, -1, " ".ljust(20 , " ") + "r = ")
        self.lbl_nueve = wx.StaticText(self, -1, "Grado 3")
        self.lbl_diez = wx.StaticText(self, -1, " ".ljust(20 , " ") + "r = ")
        self.lbl_once = wx.StaticText(self, -1, "Grado 4")
        self.lbl_doce = wx.StaticText(self, -1, " ".ljust(20 , " ") + "r = ")
        self.lbl_trece = wx.StaticText(self, -1, "Grado 5")
        self.lbl_catorce = wx.StaticText(self, -1, " ".ljust(20 , " ") + "r = ")

        # textControl
        self.text_uno = wx.TextCtrl(self, size=(50, 25))
        self.lista_text = [[], []]

        # Static line
        self.line_uno = wx.StaticLine(self, -1, size = (430, 1), style = (wx.LI_HORIZONTAL))
        self.line_dos = wx.StaticLine(self, -1, size =(430, 1), style = (wx.LI_HORIZONTAL) )

        # organizando sizers
        self.sizer_uno.Add(self.lbl_uno, wx.SizerFlags().Centre().Border(wx.ALL, 10))
        self.sizer_uno.Add(self.text_uno,  wx.SizerFlags().Centre().Border(wx.ALL, 13))
        self.sizer_uno.Add(self.btn_ok,  wx.SizerFlags().Centre().Border(wx.ALL, 13))
        self.sizer_uno.Add(self.sizer_dos, 1, wx.EXPAND)

        self.sizer_uno.Add(self.lbl_dos,wx.SizerFlags().Left().Border(wx.ALL, 13) )
        self.sizer_uno.Add(self.line_uno,wx.SizerFlags().Centre().Border(wx.ALL, 13))

        self.sizer_tres.Add(self.lbl_tres,wx.SizerFlags().Left().Border(wx.ALL, 13) )
        self.sizer_box.Add(self.sizer_tres, 1 , wx.Left)

        self.sizer_cuatro.Add(self.lbl_cuatro, wx.SizerFlags().Left().Border(wx.ALL, 13))
        self.sizer_box.Add(self.sizer_cuatro, 1 , wx.Left)

        self.sizer_uno.Add(self.sizer_box, 2, wx.EXPAND | wx.ALL, 13)
        self.sizer_uno.Add(self.line_dos, wx.SizerFlags().Centre().Border(wx.ALL, 13))

        self.sizer_cinco.Add(self.btn_calcular, wx.ALIGN_CENTER_HORIZONTAL)
        self.sizer_cinco.Add(self.btn_borrar, wx.ALIGN_CENTER_HORIZONTAL )
        self.sizer_uno.Add(self.sizer_cinco, 1, wx.ALIGN_CENTER_HORIZONTAL)

        self.sizer_uno.Add(self.lbl_cinco, wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sizer_uno.Add(self.lbl_seis, wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sizer_uno.Add(self.lbl_siete, wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sizer_uno.Add(self.lbl_ocho, wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sizer_uno.Add(self.lbl_nueve , wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sizer_uno.Add(self.lbl_diez , wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sizer_uno.Add(self.lbl_once , wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sizer_uno.Add(self.lbl_doce , wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sizer_uno.Add(self.lbl_trece , wx.SizerFlags().Left().Border(wx.ALL, 9))
        self.sizer_uno.Add(self.lbl_catorce , wx.SizerFlags().Left().Border(wx.ALL, 9))

        # eventos
        self.Bind(wx.EVT_BUTTON, self.iniciar_casillas, self.btn_ok)

        # acabados de la ventana
        self.SetSizer(self.sizer_uno)
        self.Centre()


    def iniciar_casillas(self, evt):
        try:
            n = int(self.text_uno.GetValue())

        except ValueError:
            wx.MessageDialog(None, "Digite numeros validos",
                             "SeeD.Inc Msj", wx.OK).ShowModal()
        else:  
            for _ in range(n):
                text_ctrl_uno = wx.TextCtrl(self)
                text_ctrl_dos = wx.TextCtrl(self)

                self.sizer_tres.Add(text_ctrl_uno, 1, wx.EXPAND | wx.ALL, 10)
                self.sizer_cuatro.Add(text_ctrl_dos, 1, wx.EXPAND | wx.ALL, 10)

                self.lista_text[0].append(text_ctrl_uno)
                self.lista_text[1].append(text_ctrl_dos)

            self.sizer_box.Layout()



if __name__ == "__main__":
    app = wx.App()
    v = Ventana("Ajute De Curvas")
    v.Show()
    app.MainLoop()
1
  • OMG! muchas gracias, enserio, pensé muchas otras soluciones, pero solo andaba en quimeras, se mi maestro por favor :V el 9 may. 2019 a las 4:27

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.