[wxPython] "Catching" a shutdown event

I have a simple wxPython app that is used in a school computer lab. Students log in with it, then leave it running and click a button on a small form when they are ready to leave/shut down the computer. It logs the time in and time out, among other things, in a MySQL database. What the lab director really wants is for the app to “hide” completely once the user has logged in, and when Windows shuts down to trap whatever event is available and record time out in the database, then gracefully exit. The environment is a mix of Win2K and Nt 4 in two labs. I haven’t been able to figure this out, perhaps because I’ve never gotten very deep into either wxPython or WinAPI. Any help much appreciated.

Thanks,

John Hopkins

You'll find that the app gets a Windows message on an attempt to
shutdown the app externally (other than being killed by taskman or the like, which just kill it dead w/out a message), see the wxApp docs for that.

The big problem is that you need the programme to be hidden so the user can't just kill the process with taskmanager to "log out" and get free access time. I don't know of a way to do that hiding in wxPython. I know it's possible with Win32, but I'm not sure there's a Python wrapping of the code necessary to do it. Alternately, you can disable taskman with a flag somewhere, but it's a useful utility IMO.

Another option would be to use the winlogon notification of an exit to run a "logout" script. Again, I don't know of any way to do that in pure Python.

I'd guess you could do something with creating a Service that just updates your dbase every X minutes with the currently logged-in user's name (I think you can get that from the win32api). Since the service would be system-level, the unpriveledged user can't get to it.

If you don't care about the user being able to kill the process, then the wxApp with Windows message handler should work. You could create an "at" job that auto-restarts the app if the user kills it I suppose. That's probably the lowest-effort approach? Don't know.

BTW: How do you deal with crashes/network failures/power failures/etceteras? Without an active-scan (each machine reporting every X seconds while connected), you could have someone use up all their hours just because something crashed their machine.

Sorry, no other thoughts at the moment,
Mike

John Hopkins wrote:

···

I have a simple wxPython app that is used in a school computer lab.

> Students log in with it, then leave it running and click a button on a
> small form when they are ready to leave/shut down the computer. It logs
> the time in and time out, among other things, in a MySQL database. What
> the lab director really wants is for the app to "hide" completely once
> the user has logged in, and when Windows shuts down to trap whatever
> event is available and record time out in the database, then gracefully
> exit. The environment is a mix of Win2K and Nt 4 in two labs. I
> haven't been able to figure this out, perhaps because I've never gotten
> very deep into either wxPython or WinAPI. Any help much appreciated.
>
> Thanks,
>
> John Hopkins
>

--
_______________________________________
    Mike C. Fletcher
    http://members.rogers.com/mcfletch/

I have a simple wxPython app that is used in a school computer lab.
Students log in with it, then leave it running and click a button on
a small form when they are ready to leave/shut down the computer.
It logs the time in and time out, among other things, in a MySQL
database. What the lab director really wants is for the app to
"hide" completely once the user has logged in, and when Windows
shuts down to trap whatever event is available and record time out
in the database, then gracefully exit.

I've never used them but you can try the EVT_QUERY_END_SESSION and
EVT_END_SESSION events. They may need to be attached to your wxApp object.
On the other hand, the default handlers for these events should call the
EVT_CLOSE handler for all your top-level windows, so you could probably just
handle that event like normal.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!

Thanks for the advice; here's my feeble attempt to follow it, and the
resultant traceback. I tried several combinations, but clearly not the
right one ...

Can you point out my error?

Thanks,

John

class LTApp(wxApp):
    def OnInit(self):
        frame = ChoiceFrame(NULL, -1, "Welcome! - Log In or Sign up")
        frame.Show(true)
        self.SetTopWindow(frame)

        return true

    EVT_END_SESSION(OnExit)

    def OnExit():
        print "I just Exited."
        try:
            ThisDB = MySQLdb.Connect(host=DBHost, user=DBUser, db=DBName)
            Cursor = ThisDB.cursor()
            SQLString = "UPDATE activityrecord SET LogOut = NOW() WHERE
LoginID = %s;" % self.LoginID
            Cursor.execute(SQLString)
            ThisDB.close()
        except:
            traceback.print_exc()
            pass

app = LTApp(0)
app.MainLoop()

Traceback (most recent call last):
  File "LabTracker_Dev.py", line 1710, in ?
    class LTApp(wxApp):
  File "LabTracker_Dev.py", line 1718, in LTApp
    EVT_END_SESSION(OnExit)
NameError: name 'OnExit' is not defined

···

----- Original Message -----
From: "Robin Dunn" <robin@alldunn.com>
To: <wxpython-users@lists.wxwindows.org>
Sent: Monday, June 10, 2002 9:59 AM
Subject: Re: [wxPython] "Catching" a shutdown event

> I have a simple wxPython app that is used in a school computer lab.
> Students log in with it, then leave it running and click a button on
> a small form when they are ready to leave/shut down the computer.
> It logs the time in and time out, among other things, in a MySQL
> database. What the lab director really wants is for the app to
> "hide" completely once the user has logged in, and when Windows
> shuts down to trap whatever event is available and record time out
> in the database, then gracefully exit.

I've never used them but you can try the EVT_QUERY_END_SESSION and
EVT_END_SESSION events. They may need to be attached to your wxApp

object.

On the other hand, the default handlers for these events should call the
EVT_CLOSE handler for all your top-level windows, so you could probably

just

handle that event like normal.

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwindows.org
http://lists.wxwindows.org/mailman/listinfo/wxpython-users

"John Hopkins" <john@hopkinsit.com> (JH) writes:

Thanks for the advice; here's my feeble attempt to follow it, and the
resultant traceback. I tried several combinations, but clearly not the
right one ...

Can you point out my error?

Thanks,

John

class LTApp(wxApp):
    def OnInit(self):
        frame = ChoiceFrame(NULL, -1, "Welcome! - Log In or Sign up")
        frame.Show(true)
        self.SetTopWindow(frame)

        return true

    EVT_END_SESSION(OnExit)

    def OnExit():

You can't use OnExit before it is defined (this isn't Pascal or C).
So put the EVT_END_SESSION(OnExit) after the definition. And add a
self parameter to OnExit.

···

--
Piet van Oostrum <piet@cs.uu.nl>
URL: ICS | Utrecht University [PGP]
Private email: P.van.Oostrum@hccnet.nl

The following code doesn't seem to work:

text1 = wxTextCtrl(self,-1,"")
text1.SetBackgroundColour(wxColor(255,194,197))

Could anyone help?

thanks

You forgot the "u" in "wxColour" (though you remembered it in
"SetBackgorundColour") :slight_smile:

···

On Tue, Jun 11, 2002 at 11:19:54PM +1000, richard terry wrote:

The following code doesn't seem to work:

text1 = wxTextCtrl(self,-1,"")
text1.SetBackgroundColour(wxColor(255,194,197))

Could anyone help?

Wish it were that simple, wxColor works fine for all my other controls, so
spelling not the problem.

···

On Tue, 11 Jun 2002 23:47, you wrote:

On Tue, Jun 11, 2002 at 11:19:54PM +1000, richard terry wrote:
> The following code doesn't seem to work:
>
> text1 = wxTextCtrl(self,-1,"")
> text1.SetBackgroundColour(wxColor(255,194,197))
>
> Could anyone help?

You forgot the "u" in "wxColour" (though you remembered it in
"SetBackgorundColour") :slight_smile:

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwindows.org
http://lists.wxwindows.org/mailman/listinfo/wxpython-users

...

And add a
self parameter to OnExit.

This is just where I'm getting stuck.

Tried the following, got traceback:
File "LabTracker_Dev.py", line 1730, in LTApp
    EVT_END_SESSION(self, OnExit)
NameError: name 'self' is not defined

class LTApp(wxApp):
    def OnInit(self):
        frame = ChoiceFrame(NULL, -1, "Welcome! - Log In or Sign up")
        frame.Show(true)
        self.SetTopWindow(frame)

        return true

    def OnExit(self):
        print "I just Exited."

    EVT_END_SESSION(self, OnExit)

app = LTApp(0)
app.MainLoop()

Tried the offending line this way and got the same error:
    EVT_END_SESSION(self, self.OnExit)

Tried the offending line this way:
    EVT_END_SESSION(OnExit)

Got this error, which seems consistent with the rest of wxPython, but given
the foregoing I don't know how to deal with it.
  File "LabTracker_Dev.py", line 1730, in LTApp
    EVT_END_SESSION(OnExit)
TypeError: EVT_END_SESSION() takes exactly 2 arguments (1 given)

"John Hopkins" <john@hopkinsit.com> (JH) writes:

...

And add a
self parameter to OnExit.

This is just where I'm getting stuck.

Tried the following, got traceback:
File "LabTracker_Dev.py", line 1730, in LTApp
    EVT_END_SESSION(self, OnExit)
NameError: name 'self' is not defined

class LTApp(wxApp):
    def OnInit(self):
        frame = ChoiceFrame(NULL, -1, "Welcome! - Log In or Sign up")
        frame.Show(true)
        self.SetTopWindow(frame)

        return true

    def OnExit(self):
        print "I just Exited."

    EVT_END_SESSION(self, OnExit)

Not here. Only in the definition.

···

--
Piet van Oostrum <piet@cs.uu.nl>
URL: ICS | Utrecht University [PGP]
Private email: P.van.Oostrum@hccnet.nl

"John Hopkins" <john@hopkinsit.com> writes:

Can you point out my error?

class LTApp(wxApp):
    def OnExit():

   ^^^^^^^^^^^^^^^^
      def OnExit(self):

···

--
Email: <roshiajin % yahoo.com> Yahooist Teil der Yahoo Markenfamilie

The following code doesn't seem to work:

text1 = wxTextCtrl(self,-1,"")
text1.SetBackgroundColour(wxColor(255,194,197))

It's been fixed in 2.3.3.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!