I am a wxpython newbie and would like some help on using custom classes to create and change widgets.

I have a program with 2 classes, each of which creates a panel with widgets. These panels are then added to the main frame of an application.

In one class I create a statictext label (dalabel). In another class I create a button (dabutton). I want to change the statictext label created in the first class with the button-click event created in the second class.

The following code produces the button-click error ' dalabel is not defined '.


import wx
import time

class MakePanel1(wx.Panel):
    def __init__(self, Parent, *args, **kwargs):
        wx.Panel.__init__(self, Parent, *args, **kwargs)
        self.dalabel = wx.StaticText(self, -1, "   panel 1 label   ")
        self.bs1 = wx.BoxSizer(wx.HORIZONTAL)

class MakePanel2(wx.Panel):
    def __init__(self, Parent, *args, **kwargs):
        wx.Panel.__init__(self, Parent, *args, **kwargs)
        self.dabutton = wx.Button(self, label="panel 2 button") 
        self.dabutton.Bind(wx.EVT_BUTTON, self.daclick )
        self.bs2 = wx.BoxSizer(wx.HORIZONTAL)

    def daclick(self, event=None):

class DisFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.Panel1 = MakePanel1(self)
        self.Panel2 = MakePanel2(self)

        bs = wx.BoxSizer(wx.VERTICAL)


if __name__ == '__main__':
    app = wx.App()
    frame = DisFrame(None)

What is the proper syntax for referencing the statictext object created in the MakePanel1 class whilst within the MakePanel2 class?

I would like to change the statictext label of the MakePanel1 class with the button-click event of the MakePanel2 class, but do not know how to reference this statictext object in order to change its properties.

I prefer to have the statictext and button widgets created in separate classes. Any help will be appreciated.

Thank you.

You have to reference dalabel via DisFrame --> frame --> Panel1, so it ends up frame.Panel1.dalabel.SetLabel(str(time.time()))

Thanks to vegaseat. The suggested syntax works well.

Another solution offered by faulkner of python-forum.org was to add the following code:

class MakePanel2(...):
     def __init__(...):
         self.parent = Parent
     def daclick(...):

Both solutions achieve the desired goal of changing the statictext label, but since I am a newbie, I am not in a position to determine if one approach is 'better' than the other.

Thanks again to vegaseat.

Just a matter of style! Pick the style that you think is easier to read or understand, particularly a few months from now.