Skip to main content

Ralsina.Me — Roberto Alsina's website

PyQt Quickie: QTimer

QTimer is a fair­ly sim­ple class: you use it when you want some­thing to hap­pen "in a while" or "ev­ery once in a while".

The first case is some­thing like this:

# call f() in 3 seconds
QTimer.singleShot(3000, f)

The sec­ond is this:

# Create a QTimer
timer = QTimer()
# Connect it to f
timer.timeout.connect(f)
# Call f() every 5 seconds
timer.start(5000)

Sim­ple, right? Well, yes, but it has some trick­s.

  1. You have to keep a re­f­er­ence to``­­timer``

    If you don't, it will­get garbage-­­col­lec­t­ed, and f() will nev­er be called.

  2. It may not call f() in 5 sec­ond­s.

    It will call f() more or less 5 sec­onds af­ter you en­ter the event loop. That may not be quick­ly af­ter you start the timer at al­l!

  3. You may get over­lap­ping cal­l­s.

    If f() takes long to fin­ish and re-en­ters the event loop (for ex­am­ple, by call­ing pro­ces­sEv­ents) maybe the timer will time­out and call it again be­fore it's fin­ished. That's al­most nev­er a good thing.

So, you can do this:

def f():
    try:
        # Do things
    finally:
        QTimer.singleShot(5000, f)

f()

What that snippet does, is, calls f() only once. But f itself schedules itself to run in 5 seconds. Since it does it in a finally, it will do so even if things break.

That means no overlapping calls. It also means it won't be called every 5 seconds, but 5 seconds plus whatever f takes to run. Also, no need to keep any reference to a QTimer.

Fi­nal tip: You can al­so use QTimer to do some­thing "as soon as you are in the event loop"

QTimer.singleShot(0, f)

Hope it was use­ful!

Every US presidential candidate is a moron or sleazy

  1. They all claim that evo­lu­­tion does­n't work

  2. They all claim the world is less than 10000 years old

So you have now two choic­es:

  1. They be­lieve they are say­ing the truth, which means they are mo­ron­s.

  2. They be­lieve they are ly­ing, and on­­ly say­ing ob­vi­ous­­ly false things be­­cause that will help them win, which means they are sleazy.

Have a fun fu­ture, US!

Antonio María Delgado is an ignorant

An­to­nio María Del­ga­do writes for the Mi­a­mi New Her­ald. I would dare guess he's sup­posed to know en­glish, even if he writes in span­ish.

Let's back­track a bit. The Econ­o­mist pub­lish­es (as it has done for a while), some­thing called the "Mis­ery In­dex", which at­tempts to cap­ture the coun­tries where peo­ple are feel­ing mis­er­able, and rank them.

So, Mr. Del­ga­do goes, reads that, sees Venezuela near the top, and I can imag­ine the idea light­bulb bright­en­ing to a daz­zling 0.5 watts, his scream of "po­lit­i­cal an­gle!" and the rush to write this:

"Venezuela se ubicó el año pasa­do en el se­gun­do puesto del "Indice de Mis­e­ri­a" elab­o­ra­do por la re­vista británi­ca The Econ­o­mist..."

The prob­lem, dear eng­lish read­er, is that "mis­e­ri­a" does­n't mean "mis­ery". It means "pover­ty". In fac­t, it means "ex­treme pover­ty". And thus, Mr. Del­ga­do makes it seem as if the Econ­o­mist is say­ing Venezuela is the sec­ond poor­est coun­try (in some sense).

While I per­son­al­ly dis­like Chavez, this is ei­ther ig­no­ran­t, or stupid. Your cal­l.

Demetrio Fernandez is an idiot

Spain is a coun­try. Cór­do­ba is a place in Spain. In Cór­do­ba there are church­es. Peo­ple go to church­es. The peo­ple who at­tend church in Cór­doba, Spain, are un­der the spir­i­tu­al guid­ance of Demetrio Fer­nan­dez, bish­op.

Just so we are clear: You are sup­posed to greet him by kiss­ing his hand.

On the oth­er hand, Demetrio Fer­nan­dez is an id­iot who said this:

"The Min­is­ter for Fam­i­ly of the Pa­pal Gov­ern­men­t, Car­di­nal An­tonel­li, told me a few days ago in Zaragoza that UN­ESCO has a pro­gram for the next 20 years to make half the world pop­u­la­tion ho­mo­sex­u­al. To do this they have dis­tinct pro­gram­s, and will con­tin­ue to im­plant the ide­ol­o­gy that is al­ready present in our school­s."

I rest my case. Demetrio Fer­nan­dez is an id­iot. Car­di­nal An­tonel­li is an id­iot. And if you go to church in Cór­doba, Spain, know­ing about this, and kiss the id­iot's hand, you are an id­iot.

Idioms are not your friends

Us­ing id­ioms is a dou­ble-edged sword. One side lets you cut to the point. You say a phrase, and it's so load­ed of mean­ing, it's a short­cut to what you mean. One side can cut you, if you just use it and you use it wrong.

Let's con­sid­er Beat­riz Sar­lo, a re­spect­ed ar­gen­tine in­t­elec­tu­al. She writes op-ed col­umns in a news­pa­per. She wrote one to­day. It end­s:

To­tal, Boudou, sin bromi­ta al­gu­na, debe ade­cuarse a lo que le to­que, obe­de­cien­do el viejo re­frán de que a un ca­bal­lo re­gal­a­do no hay que ex­am­i­narlo para ver si viene com­ple­to.

Which I trans­late as:

In any case, Boudou, jokes aside, has to ac­cept what he get­s, fol­low­ingth­at old ad­vice about not ex­am­in­ing gift hors­es to see if they are whole.

One pe­cu­liar thing here is that we have al­most the ex­act same say­ing in span­ish and eng­lish. In eng­lish you don't look gift hors­es in the mouth. In span­ish you don't check the teeth of gift hors­es. The prob­lem here (if I may be so pedan­tic (yes you may (thanks oth­er me! (my plea­sure)))) is that Ms. Sar­lo has no freak­ing clue of what that mean­s.

Sup­pose you are buy­ing a horse. You would check the mouth be­cause you want to see if the horse is young or old (c­i­ta­tion need­ed? Here is google's first re­sult). That's im­por­tant when you buy a horse. It's, on the oth­er hand, re­al­ly use­less and rude when you are get­ting a horse as a gift:

Nice guy: Hey, here's a horse!

Rude mo­ron: (looks in the mouth) No thanks!

That's why you don look gift hors­es in the mouth. And that's al­so why you don't check ama­zon to see how much the book you got cost­s.

On the ther hand, if you were a Tamar­i­an, you could look at the horse's mouth and say "Tem­ba, at rest".


Contents © 2000-2020 Roberto Alsina