0

Is there a way, from a button to reinitialize a specific panel? I have a section of an app that looks for specific files in the OS and then creates some checkboxes and textctrls and then adds them to the sizer dynamically:

    for db in numDB:
        if xIndex >= 12:
            pass
            xIndex += 1
        else:
            check = wx.CheckBox(self, -1, db)
            sizer.Add(check, pos=(xIndex,0), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
            label = wx.StaticText(panel, label="")
            sizer.Add(label, pos=(xIndex,1), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
            name = wx.TextCtrl(panel)
            #Set Temp Name
            if db.endswith('.db'):
                name.Value = db[:-3]
            sizer.Add(name, pos=(xIndex,2), span=(1,3),flag=wx.EXPAND|wx.RIGHT|wx.ALIGN_CENTER_VERTICAL, border=10)
            xIndex +=1

I have a refresh button, in the event the user adds a new db, or removes one. The simplest concept for me, at least, is to try to find a way to rerun the init.

I'm trying to reference the init by:

def onRefreshClick(self, event):
    self.__init__

That def happens within the Class itself, but it doesn't seem to do anything. And I'm super new to the whole wxPython and Python in general, so it's probably something simple that I cannot quite figure out.

Edited to try and incorporate super():

class LaunchPanel(wx.Panel):
    """
    Launch Tab for finding and launching .db files
    """
    #----------------------------------------------------------------------
    def __init__(self, parent):
        """"""
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
        super (LaunchPanel)
        self.initialize()

    def initialize(self):
        panel = self
        sizer = wx.GridBagSizer(11, 3)
        <snip...>

This results in my panel being empty. When I hit refresh, it adds (I think) all my elements to the upper left portion of the panel...? I'm sure it's easy, whatever I am doing wrong...?

chow
  • 484
  • 2
  • 8
  • 21

2 Answers2

2

You're missing parentheses:

self.__init__() # now, this is a method call
Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83
  • So, I get an error where it is looking for two arguments -- which the def looks like this: def __init__(self, parent) for the init method itself. I tried passing it self.__init__(self) and I got a critical python error. :P – chow Jul 23 '11 at 17:29
  • Don't pass `self`, that happens automatically when the method is bound to an instance. Just pass in the second argument `parent`. – Eric Wilson Jul 23 '11 at 18:00
1

A simple solution is to have your __init__ do nothing but call another method, then you can call that method whenever you want:

def __init__(self, ...):
    super(...)
    self.initialize()

def initialize(self):
    <actual intialization code here>

I think that's a lot cleaner than directly calling __init__. Plus, this lets you separate stuff that must truly be done only at object instantiation (such as calling the init of the superclass) from the code you want to call multiple times.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • This is where I get really embarassed -- what do I put inside the super(...)? And def initialize(self): should be at the same indentation level as the `__init__`? – chow Jul 24 '11 at 17:34
  • @chow: documentation for `super` can be found here among other places: http://docs.python.org/library/functions.html#super and yes, `def initialization` goes at the same level of indentation as `def __init__`. – Bryan Oakley Jul 24 '11 at 19:22