创建面板后运行 __init__ 吗?

发布于 2024-11-26 01:37:56 字数 1651 浏览 2 评论 0原文

有没有办法通过按钮重新初始化特定面板?我有一个应用程序的一部分,它在操作系统中查找特定文件,然后创建一些复选框和文本控制,然后将它们动态添加到 sizer 中:

    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

我有一个刷新按钮,以防用户添加新数据库或删除一个数据库。至少对我来说,最简单的概念是尝试找到一种重新运行 init 的方法。

我试图通过以下方式引用 init

def onRefreshClick(self, event):
    self.__init__

def 发生在类本身内,但它似乎没有做任何事情。总的来说,我对整个 wxPython 和 Python 都非常陌生,所以这可能是一些我不太明白的简单事情。

编辑以尝试合并 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...>

这会导致我的面板为空。当我点击刷新时,它会将(我认为)所有元素添加到面板的左上部分......?我确信这很容易,无论我做错了什么......?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○闲身 2024-12-03 01:37:56

你缺少括号:

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

You're missing parentheses:

self.__init__() # now, this is a method call
棒棒糖 2024-12-03 01:37:56

一个简单的解决方案是让您的 __init__ 不执行任何操作,只调用另一个方法,然后您可以随时调用该方法:

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

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

我认为这比直接调用 __init__ 干净得多。另外,这可以让您将那些必须真正在对象实例化时完成的内容(例如调用超类的 init )与您想要多次调用的代码分开。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文