Python tkinter Capture windows system shutdown - python

I'm trying to get my tkinter app (python 3.4.2) to be aware of the system shutdown event so it can release the sqlite3 connection and close a log. I found a post from 2009 about using the win32 api module. I can't get the posted sample to work as I expect (I may not understand it), where a message should cause the wndproc function to fire.
2009 reference:
Python - Windows Shutdown Events
Any other good references or pointers to how to accomplish this?

Typically you want to close the connection anytime the app closes, even if it wasn't a normal closure (ie: by picking "Exit" from a menu). The normal way to do that is to set up a handler for the WM_DELETE_WINDOW protocol (something of a dinosaur left over from when tk only worked on X11 systems). I don't know for certain your app will be notified this way when the system shuts down, but it probably does.
For more information see this question on stackoverflow: Intercept Tkinter "Exit" command?

Related

window message not received using python win32api

I am using win32 packages to send messages to a window. I manage to get the handle and apparently the message is sent as I get return value 0 from send code. however it has no action in the window, and spyxx does not see the message on the specified handle.
my window has no child window and is class UnityWndClass. (I believe it uses a 64 bit process as it was not visible with spy++?)
Running the IDE as admin made it work (the program also requires admin)

Tkinter wait for tcp input/output

I wrote a networked chatroom server and client and it works in a simple Terminal environment just fin, and just today started to translate it to over to a GUI form using Tkinter. The sending of messages is fine as I can handle them with button press event handlers and key handles, but the problem is receiving messages. I need to be able to check to see if data has been received while still in the Tkinter window. Is there a nice way of doing something like this? I've tried checking every second using the root.action(time,event) call, but that didn't seem to work, and have just tried running loops in different spots (against my better judgement).
What can I do to have Tkinter listen to something outside of itself, but still be listening to the events going on within my Tkinter window?
See
How do you run your own code alongside Tkinter's event loop?
In particular, the comment about setting the timeout to 0 in the Tk().after() call, so you have non-blocking, outside the Tk event loop code handling possibilities.
Hope that helps.

PyQt cross-platform way of communicating between running applications

Hey guys/ladies of python mastery, need some help.
I'm using PyQt to create some integrated ui elements for an application written in python (appA) that runs on windows/linux/osx, I need those elements to be able to communicate with a PyQt app (appB) I have written that runs separately (same host).
What would be the best (cross-platform) approach to creating a communication link between the integrated ui elements in one app and the standalone app i have written?
I want to be able to send/receive messages from appA to appB...
For example appA launches, integrated ui elements load in a form of an input field with a 'Send' button. Upon entering data and hitting 'Send' a test is performed to check if appB is running/needs to be launched, after appB launches, data arrives at appB and appB sends a confirmation of delivery back to appA and vice versa. This needs to work with least delay and hackery on windows/linux/osx.
I was looking at DBus but that looks a little buggy for WIN, win32api is good for WIN but useless elsewhere, maybe there is a magic bullet to these things.
Any links to tutorials/sites/docs would be great or if u have some ready code :) ! whatever , thanks in advance...
If you don't want to use raw sockets, you should try zmq (zeromq). You can find a good introduction to zmq here

Python desktop software with web interface

I am building desktop software with a Python backend and a web interface. Currently, I have to start the backend, then open up the interface in the browser. If I accidentally refresh the page - then that clears everything! What I'd like to do is start the application and have a fullscreen browser window appear (using Chrome) - that shouldn't be difficult. I have two questions:
Can refresh be disabled?
Is it possible to hook into closing my program when the web UI is closed?
Update:
What I'm looking for is more like this: geckofx. A way to embed a Chrome webpage in a desktop app. Except I'm using Python rather than C#
Your first question is a dup of disable f5 and browser refresh using javascript.
For your second question, it depends on what kind of application you're building, but generally, the answer is no.
If you can rely on the JS to catch the close and, e.g., send a "quit" message to the service before closing, that's easy. However, there are plenty of cases where that won't work, because the client has no way to catch the close.
If, on the other hand, you can rely on a continuous connection between the client and the service—e.g., using a WebSocket—you can just quit when the connection goes down. But usually, you can't.
Finally, if you're writing something specifically for one platform, you may be able to use OS-level information to handle the cases that JS can't. (For example, on OS X, you can attach a listener to the default NSDistributedNotificationCenter and be notified whenever Chrome quits.) But generally, you can't rely on this, and even when you can, it may still not cover 100% of the cases.
So, you need to use the same tricks that every "real" web app uses for managing sessions. For example, the client can send a keepalive every 5 minutes, and the server can quit if it doesn't get any requests for 5 minutes. (You can still have an explicit "quit" command, you just can't rely on always receiving it.) If you want more information on ways to do this, there are probably 300 questions on SO about it.
Instead of embeding Chrome, you may embed only Webkit ( I don't know on Windows, but on Mac and Linux is easy).
Application logic seams to be on server side, and browser used only as interface. If that is the case, you may put „onbeforeunload” in body tag, and call a js function that send an ajax request to server to die.

Listening to network event and keyboard input at the same time in Python

I want to write an (GUI) application that listens both to keyboard events (client side generated events) and to a network port (server side generated events). I could use some high level advice on how to do this. Some additional info:
- I am using the wxPython module for the GUI
- I could set the socket in non-blocking mode, but this way I have to keep polling the socket by keeping executing the recv() command. I did this earlier and I can recall that this used considerable resources
- I could use the thread module, but since I am not familiar with it, I try to avoid this, but maybe I can't
Advice would be appreciated.
wxPython does have key events. Here are the wxPython docs page on the subject: http://www.wxpython.org/docs/api/wx.KeyEvent-class.html
wxPython doesn't wrap every single thing in wxWidgets. The developers didn't think that they needed to wrap stuff that already had great support in Python itself. Thus, see Python for its socket support
http://docs.python.org/library/socket.html
http://docs.python.org/howto/sockets.html
http://docs.python.org/library/socketserver.html
And if you want to get really heavy, look into the Twisted framework. There are several articles on using it with wxPython:
http://twistedmatrix.com/documents/current/core/howto/choosing-reactor.html
http://wiki.wxpython.org/wxPythonAndTwisted
http://code.activestate.com/recipes/181780-using-wxpython-with-twisted-python/
I am not a wx expert. Could you use wx's native event driven mechanisms? The keypress would certainly have an event. Wx has a socket class wxSocketClient() that could translate the low level socket events (data ready, closed, etc) into a wx event.
There is wxKeyEvent in Docs. Use that to catch the keys and Socket to send it via a network or do whatever you want to do! For socket see this sticky. It is C++ but it will give you a better Idea!

Categories

Resources