Examples: LayoutConstraints.py

In LayoutConstraints.py how do I make it work without the demo? If I copy the example file to another directory and try to run it, I get:

ModuleNotFoundError: No module named 'run'

Usually with wxPython examples, the last lines are something like this:

app = wx.App()
frm = TestFrame(None, title="Title", size=(700,500))
frm.Show()
app.MainLoop()

But I don’t see any “frame” in that code I could refer to.

The modules in the demo are designed to be loadable by the simple framework that the demo application provides, and although they can be run mostly standalone within the demo folder, they are not intended to be pulled out and run standalone without the demo framework. The code is meant to be used as an example of how things work, but not necessarily as a starting point for new applications.

That said, it really isn’t hard to do so. In this case you would just need to remove everything from def runTest to the end, and add code that creates a frame that uses TestLayoutConstraints as its content, creates a wx.App and calls MainLoop)

I think I got it. I added the below code to the end of the program (after removing what you said to remove) and I don’t see errors. It just feels complex at the beginning when you still feel confused about Frames, Panels, Windows, sizers, their relations and so on. Looking at different examples from the Web can make it more complex in your mind. Some examples don’t have much of def’s (functions) and classes as the most simple examples don’t need so much code.

class Frame(wx.Frame):

    def __init__(self, parent, title, style, name):

        super(Frame, self).__init__(parent, title=title, style=style, name=name)

        self.InitUI()
        self.Centre()


    def InitUI(self):

        panel = TestLayoutConstraints(self)


def main():

    app = wx.App(redirect=False, filename=None, useBestVisual=False, clearSigInt=True)
    frame = Frame(None, title="LayoutConstraints", style=wx.DEFAULT_FRAME_STYLE, name="LayoutConstraints")
    frame.Show()
    app.MainLoop()


if __name__ == '__main__':
    main()