user3313622
user3313622

Reputation: 15

Using a panel from another panel in wxpython

I'm new using wxpython.

I created some code to generate a splitter window, in the left panel I created a listbox and in the right panel I created a button.

What I would like to do is that when I press my button in the right panel, my list in the left panel display the string "Hello World".

I've tried the following:

import wx

########################################################################
class LeftPanel(wx.Panel):
""""""

    #----------------------------------------------------------------------
    def __init__(self, parent):
    """Constructor"""
       wx.Panel.__init__(self, parent=parent)

           self.lizt = wx.ListBox(self, -1, pos = wx.DefaultPosition, size = (300,     120), choices = "", style = wx.LB_SINGLE|wx.LB_HSCROLL|wx.LB_SORT, name = "aDB")

           sizer = wx.BoxSizer(wx.VERTICAL)
           sizer.Add(self.lizt, 0, wx.EXPAND)
           self.SetSizer(sizer)

########################################################################
class RightPanel(wx.Panel):
    """"""

   #----------------------------------------------------------------------
    def __init__(self, parent):
    """Constructor"""
        wx.Panel.__init__(self, parent=parent)
            txt = wx.Button(self, wx.ID_ANY, "txt") 
            txt.SetLabel("ALL")
            txt.Bind(wx.EVT_BUTTON, self.write, txt)

    def write(self, event): 
        LeftPanel.lizt.Clear()
        LeftPanel.lizt.Append("HELLO WORLD")
        return

########################################################################
class MyForm(wx.Frame):

#----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, title="Splitter Tutorial")

        splitter = wx.SplitterWindow(self)
        leftP = LeftPanel(splitter)
        rightP = RightPanel(splitter)

        # split the window
        splitter.SplitVertically(leftP, rightP)
        splitter.SetSashGravity(0.5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(splitter, 1, wx.EXPAND)
        self.SetSizer(sizer)

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

But I get this Error: AttributeError: type object 'LeftPanel' has no attribute 'lizt'

What am I doing wrong :(

Thanks in advance

Upvotes: 0

Views: 841

Answers (1)

user2682863
user2682863

Reputation: 3218

You're referencing the 'LeftPanel' class and not the instance of that class. 'lizt' doesn't exist until you create an instance of the 'LeftPanel' class. To reference the left panel from the right panel you must first create a persistent reference to the left panel in the main frame.

instead of

leftP = LeftPanel(splitter)

do

self.leftP = LeftPanel(splitter)

then from the right panel's 'write' method you have to find the reference to the left panel.

parent = self.GetParent()
left_panel = parent.leftP
left_panel_list = left_panel.lizt

There's obviously easier ways to save references, you could explicitly pass the left panel's list to the right panel's init method. Or more preferably move everything to the init method of the frame and save all references to the frame's instance. As a general tip, remember the local variables in your init methods go away when the init method is done unless you explicitly save them to the class. If you're planning on referencing the lists, panels, etc later, save the references to the class (self.leftP instead of just leftP)

Upvotes: 1

Related Questions