Hi guys,
I've written a couple of program modules ,one main program that imports the other.
The problem is when I run the imported module it's fine but on exiting it closes my main program with it.
Both modules work ok otherwise, also I can run them individually as well.
Any suggestions would be greatly appreciated.

Thanks

Dani AI

Generated

This thread shows a classic GUI-import problem reported by . The usual cause is that the imported module is terminating the whole interpreter when its window closes — most often because it starts or stops the application loop or explicitly exits the process. As hinted, how the modules are launched makes the difference; the imported file should define widgets and classes only, and avoid running an app loop or calling exit when imported.

Checklist of the common fixes:

  • Do not call sys.exit() or os._exit() from an imported module or from an EVT_CLOSE handler. Those end the whole process.
  • Ensure only one wx.App is created for the whole program. Creating a second wx.App or calling MainLoop() inside the imported module will interfere with the main app.
  • Put any standalone-run code under if __name__ == "__main__": so importing does not execute it.
  • In close handlers prefer event.Skip() and Destroy() rather than calling exit functions.
  • Keep a strong reference to child windows (assign to an attribute) so they are not garbage-collected unexpectedly.

Minimal patterns to follow:

Module (child module):

import wx

class ChildFrame(wx.Frame):
    def __init__(self, parent=None):
        wx.Frame.__init__(self, parent, title="Child")
        # build widgets here

if __name__ == "__main__":
    app = wx.App(False)
    f = ChildFrame(None)
    f.Show()
    app.MainLoop()

Main program:

import wx
from child import ChildFrame

app = wx.App(False)
main = wx.Frame(None, title="Main")
main.Show()
main.child = ChildFrame(main)
main.child.Show()
app.MainLoop()

Additional notes: search the imported module for any wx.App, MainLoop, or sys.exit calls. Add logging or a try/except around the child-window code to catch SystemExit when reproducing the problem. Following the single-wx.App and if __name__ == "__main__" patterns resolves this issue in nearly all cases.

Recommended Answers

All 3 Replies

I think it would be easier if you told us how you run your programs, and may be which code they contain !

Sorry Gribouillis,
I'm using python2.5 .
The GUI is wxGlade and using Eric4 for the coding.

I think it would be easier if you told us how you run your programs, and may be which code they contain !

Sorry Gribouillis,
I'm using python2.5 .
The GUI is wxGlade and using Eric4 for the coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.