self-closing window with wxPython

Jabba Laci jabba.laci at gmail.com
Fri Sep 17 12:05:18 EDT 2010


Hi,

I'd like to create a simple alarm application that shows an alarm
window. The application should shut down automatically after 5
seconds. The problem is the following:
* If I keep the mouse outside of the window, the application keeps
running. Somehow self.Destroy() is not taken into account.
* If the mouse is over the window and I keep moving it, the window closes.

I'm using Ubuntu Linux with wxPython 2.8. Below you can find what I have so far.

Thanks,

Laszlo

==========

class MyThread(threading.Thread):
    def __init__(self, parent):
        self.parent = parent
        threading.Thread.__init__(self)

    def run(self):
        print time.time()    # appears on stdout
        time.sleep(5)
        print time.time()    # appears on stdout

        self.parent.Destroy()    # ??? doesn't work if the mouse is
outside of the application window

class Alarm(wx.Frame):
    def __init__(self, title, *args):
        wx.Frame.__init__(self, None, -1, title, pos=(0, 0),
size=(800, 600), *args)

        self.sleepThread = MyThread(self)
        self.sleepThread.start()

        self.Bind(wx.EVT_CLOSE, self.on_close)

    def on_close(self, event):
        self.Destroy()

==========

To call it:

class Main(wx.PySimpleApp):
        def OnInit(self):
            self.frame = alarm.Alarm("Alarm 0.1")
            self.SetTopWindow(self.frame)
            self.SetExitOnFrameDelete(True)
            self.frame.Show()
            return True

    a = Main()
    a.MainLoop()

=====



More information about the Python-list mailing list