Run __init__ after panel is created?

僤鯓⒐⒋嵵緔 提交于 2020-01-07 07:16:08

问题


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...?


回答1:


You're missing parentheses:

self.__init__() # now, this is a method call



回答2:


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.



来源:https://stackoverflow.com/questions/6801949/run-init-after-panel-is-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!