pedram
pedram

Reputation: 3087

How can I modify my wxPython code live to avoid closing/reopening them?

Here's an example workflow for wxPython programs: write some code, run the app, notice that my border sizes are off by a few pixels, close the program, make minor adjustments, repeat until it looks just right.

This is obviously inefficient, and I have countless other cases where I'm making minor tweaks to the interface which would be a lot easier if I didn't have to close/reopen my program a dozen times.

I'm currently using IDLE but am open to alternatives. Is there a better way? I've seen it done in the wxPython Demo app where you can make changes to the code and see the results right away, but I'm not clear on how that's done.

Upvotes: 2

Views: 194

Answers (1)

Mike Driscoll
Mike Driscoll

Reputation: 33071

You can use Python's reload() built-in functionality. Here's a fun little demo:

import testApp
import wx

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

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

        showAppBtn = wx.Button(self, label="Show App")
        showAppBtn.Bind(wx.EVT_BUTTON, self.onShowApp)

        reloadBtn = wx.Button(self, label="Reload")
        reloadBtn.Bind(wx.EVT_BUTTON, self.onReload)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(showAppBtn, 0, wx.ALL|wx.CENTER, 5)
        mainSizer.Add(reloadBtn, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizer(mainSizer)

    #----------------------------------------------------------------------
    def onReload(self, event):
        """
        Reload the code!
        """
        if self.testFrame:
            self.testFrame.Close()
            reload(testApp)
            self.showApp()
        else:
            self.testFrame = None

    #----------------------------------------------------------------------
    def onShowApp(self, event):
        """
        Show the app
        """
        self.showApp()

    #----------------------------------------------------------------------
    def showApp(self):
        """
        """
        self.testFrame = testApp.TestFrame()

########################################################################
class ReloaderFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Reloader")
        panel = ReloaderPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = ReloaderFrame()
    app.MainLoop()

And here's the testApp script that you can edit:

import wx

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

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

########################################################################
class TestFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test program")
        panel = TestPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = TestFrame()
    app.MainLoop()

Make sure you save the second one as testApp.py. Now if you edit this second script and hit the Reload button in the first script, you will see your changes.

Upvotes: 3

Related Questions