Foros del Web » Programando para Internet » Python »

[SOLUCIONADO] Es posible llamar resultado de variable dentro de una clase distinta

Estas en el tema de Es posible llamar resultado de variable dentro de una clase distinta en el foro de Python en Foros del Web. Buenas, estoy intentando cambiar un Label que esta dentro de una clase con una accion que esta en otra clase. Estoy con wxpython y utilizando ...
  #1 (permalink)  
Antiguo 05/04/2015, 18:52
 
Fecha de Ingreso: abril-2015
Ubicación: Canarias
Mensajes: 7
Antigüedad: 9 años
Puntos: 0
Es posible llamar resultado de variable dentro de una clase distinta

Buenas, estoy intentando cambiar un Label que esta dentro de una clase con una accion que esta en otra clase.
Estoy con wxpython y utilizando 2 paneles en una misma ventana.

Código:
import wx
    #----------------------------------------------------------------------


class RightPanel(wx.Panel):
 
    def __init__(self, parent):

        wx.Panel.__init__(self, parent=parent)
     
        self.texto = wx.StaticText(self, -1, 'Test',(230,10), style=wx.ALIGN_CENTRE) #<------ Label que quiero cambiar

    #----------------------------------------------------------------------

class LeftPanel(RightPanel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)
        
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        vbox = wx.BoxSizer(wx.VERTICAL)

        panel1 = wx.Panel(self, -1)
        self.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (-1,-1), wx.TR_HIDE_ROOT|wx.TR_DEFAULT_STYLE)

        root = self.tree.AddRoot('Programmer')
        os = self.tree.AppendItem(root, 'Programas')
        pl = self.tree.AppendItem(root, 'Programas en Python 3.4')

        self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.Cambiar_Texto_Label, id=1)
        vbox.Add(self.tree, -1, wx.EXPAND)
        hbox.Add(panel1, -1, wx.EXPAND)
        panel1.SetSizer(vbox)
        self.SetSizer(hbox)
        self.Centre()
        
    def Cambiar_Texto_Label(self, event): #<------- Accion para cambiar label

        item =  event.GetItem()
        self.texto.SetValue(item) #<------- Accion para cambiar Label



    #----------------------------------------------------------------------

class Cagador_Ventana(wx.Frame):

 
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,
                          "Panel",
                          size=(1100,660))
 
        splitter = wx.SplitterWindow(self)
        leftP = LeftPanel(splitter)
        rightP = RightPanel(splitter)
 

        splitter.SplitVertically(leftP, rightP)
        splitter.SetMinimumPaneSize(250)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(splitter, 1, wx.EXPAND)
        self.SetSizer(sizer)
        self.Centre()
#----------------------------------------------------------------------

if __name__ == "__main__":
    app = wx.App(False)
    frame = Cagador_Ventana()
    frame.Show()
    app.MainLoop()
    app.MainLoop()

Última edición por razpeitia; 05/04/2015 a las 19:11
  #2 (permalink)  
Antiguo 05/04/2015, 19:51
Avatar de razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Es posible llamar resultado de variable dentro de una clase distinta

Es bastante simple, le agregas un callback y dejas que ese callback que esta en la clase que contiene el panel derecho e izquierdo, maneje la situación.

Código Python:
Ver original
  1. import wx
  2.  
  3.  
  4. class RightPanel(wx.Panel):
  5.     def __init__(self, parent):
  6.         wx.Panel.__init__(self, parent=parent)
  7.         self.texto = wx.StaticText(self, -1, 'Test', (230,10), style=wx.ALIGN_CENTRE) #<------ Label que quiero cambiar
  8.  
  9.     def setTitle(self, title):
  10.         self.texto.SetLabel(title)
  11.  
  12.  
  13. class LeftPanel(wx.Panel):
  14.     def __init__(self, parent, parent_callback):
  15.         wx.Panel.__init__(self, parent=parent)
  16.  
  17.         self.parent_callback = parent_callback
  18.        
  19.         hbox = wx.BoxSizer(wx.HORIZONTAL)
  20.         vbox = wx.BoxSizer(wx.VERTICAL)
  21.  
  22.         panel1 = wx.Panel(self, -1)
  23.         self.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (-1,-1), wx.TR_HIDE_ROOT|wx.TR_DEFAULT_STYLE)
  24.  
  25.         root = self.tree.AddRoot('Programmer')
  26.         os = self.tree.AppendItem(root, 'Programas')
  27.         pl = self.tree.AppendItem(root, 'Programas en Python 3.4')
  28.  
  29.         self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.Cambiar_Texto_Label, id=1)
  30.         vbox.Add(self.tree, -1, wx.EXPAND)
  31.         hbox.Add(panel1, -1, wx.EXPAND)
  32.         panel1.SetSizer(vbox)
  33.         self.SetSizer(hbox)
  34.         self.Centre()
  35.        
  36.     def Cambiar_Texto_Label(self, event): #<------- Accion para cambiar label
  37.         item =  event.GetItem()
  38.         text = self.tree.GetItemText(item)
  39.         self.parent_callback(text)
  40.  
  41.  
  42. class Cagador_Ventana(wx.Frame):
  43.     def __init__(self):
  44.         wx.Frame.__init__(self, None, wx.ID_ANY,
  45.                           "Panel",
  46.                           size=(1100,660))
  47.  
  48.         splitter = wx.SplitterWindow(self)
  49.         self.leftP = LeftPanel(splitter, self.parent_callback)
  50.         self.rightP = RightPanel(splitter)
  51.  
  52.  
  53.         splitter.SplitVertically(self.leftP, self.rightP)
  54.         splitter.SetMinimumPaneSize(250)
  55.         sizer = wx.BoxSizer(wx.VERTICAL)
  56.         sizer.Add(splitter, 1, wx.EXPAND)
  57.         self.SetSizer(sizer)
  58.         self.Centre()
  59.         self.Layout()
  60.  
  61.     def parent_callback(self, data):
  62.         self.rightP.setTitle(data)
  63.  
  64.  
  65. if __name__ == "__main__":
  66.     app = wx.App(False)
  67.     frame = Cagador_Ventana()
  68.     frame.Show()
  69.     app.MainLoop()
  #3 (permalink)  
Antiguo 06/04/2015, 04:14
 
Fecha de Ingreso: abril-2015
Ubicación: Canarias
Mensajes: 7
Antigüedad: 9 años
Puntos: 0
Respuesta: Es posible llamar resultado de variable dentro de una clase distinta

Gracias, ahora lo entendí.
Aun no conocía la función callback.

Gracias :)

Etiquetas: class, wxpython
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:49.