# llamar f() en 3 segundos
El segundo es así:
# Creamos un QTimer
timer = QTimer()
# Lo conectamos a f
# Llamamos a f() cada 5 segundos
¿Fácil, no? Bueno, sí, pero tiene un par de trampas.
Hay que guardar la referencia a timer
Si no, lo recoge el basurero, y nunca se llama a f()
Capaz que son más de 5 segundos
Va a llamar a f() más o menos cada 5 segundos después de que entre al event loop. Tal vez eso no sea enseguida después de que arrancás el timer!
Capaz que se pisan las llamadas
Si f() tarda mucho en terminar, y vuelve a entrar al event loop (por ejemplo, llamando a processEvents) tal vez timer se dispare antes que f() termine, y la llame de nuevo. Eso casi nunca es buena idea.
I have written about this in the past, with the general conclusion being "it's a pain in the ass".
So, now, here is how it's done.
Start with a working PyQt application. In this example, I will use devicenzo.py mostly because:
It is a working PyQt application.
It uses a big chunk of PyQt
It's easy to test
Now you need a setup.py. Here's one that works, with extensive commments.
# We will be using py2exe to build the binaries.
# You may use other tools, but I know this one.
from distutils.core import setup
# Now you need to pass arguments to setup
# windows is a list of scripts that have their own UI and
# thus don't need to run in a console.
# And now, configure py2exe by passing more options;
# This is magic: if you don't add these, your .exe may
# or may not work on older/newer versions of windows.
# Py2exe will not figure out that you need these on its own.
# You may need one, the other, or both.
# Optional: make one big exe with everything in it, or
# a folder with many things in it. Your choice
# 'bundle_files': 1,
# Qt's dynamically loaded plugins and py2exe really don't
# get along.
data_files = [
# If you choose the bundle above, you may want to use this, too.
Run python setup.py py2exe and get a dist folder full of binary goodness.
And that's it. Except of course, that's not it.
What this will do is create a binary set, either a folder full of things, or a single EXE file. And that's not enough. You have to consider at least the following:
Put everything in resource files: images, qss files, icons, etc. Every file your app needs? Put it in a resource file and load it from there. That way you don't have to care about them if you go the "one exe" road.
Compile .ui files to .py (same reason)
Figure out if you use Qt's plugins, and make them work. This includes: using Phonon, using QtSQL, and using any image formats other than PNG.
After you have that, are you done? NO!
Your windows user will want an installer. I am not going to go into details, but I had a good time using BitRock's InstallBuilder for Qt. It's a nice tool, and it works. That's a lot in this field.
But is that all? NO!
You have to take care of the Visual Studio Runtime. My suggestion? Get a copy of the 1.1MB vcredist_x86.exe (not the larger one, the 1.1MB one), and either tell people to install it manually, or add it to your
installer. You are legally allowed (AFAIK) to redistribute that thing as a whole. But not what's in it (unless you have a VS license).
And we are done? NO!
Once you run your app "installed", if it ever prints anything to stderr, you will get either a dialog telling you it did, or worse (if you are in aything newer than XP), a dialog telling you it can't write to a log file, and the app will never work again.
This is because py2exe catches stderr and tries to save it on a logfile. Which it tries to create in the same folder as the binary. Which is usually not allowed because of permissions.
Solution? Your app should never write to stderr. Write an excepthook and catch that. And then remove stderr or replace it with a log file, or something. Just don't let py2exe do it, because the way py2exe does it is broken.
And is that it?
Well, basically yes. Of course you should get 4 or 5 different versions of windows to test it on, but you are pretty much free to ship your app as you wish. Oh, mind you, don't upload it to downloads.com because they will wrap your installer in a larger one that installs bloatware and crap.
Esta cosa es apenas una capa de pintura encima de Disqus! Más un blog sin posts pero con comentarios que un foro! Pero... qué le falta para ser un foro de verdad? Funciona, no? Hasta se podrían usar categorías de Disqus para crear subforos...
Teniendo todo en cuenta, creo que es un hack bonito.
Y si esperás unos días, esto lleva a otra cosa que es mucho más mágica...