1

So I've created a class, which contains GUI using wxPython. How do you make it so that it refreshes itself say every minute?

jewz
  • 71
  • 1
  • 1
  • 3

3 Answers3

2

For things that happen on intervals, use a Timer. From WxPyWiki:

def on_timer(event):
    pass  # do whatever

TIMER_ID = 100  # pick a number
timer = wx.Timer(panel, TIMER_ID)  # message will be sent to the panel
timer.Start(100)  # x100 milliseconds
wx.EVT_TIMER(panel, TIMER_ID, on_timer)  # call the on_timer function

For some reason, this code didn't work when I tried it. The reason was the timer had to be a class member. If you put that code into the init() method and add self. before timer, it should work. If it doesn't, try making on_timer() a class member too. -- PabloAntonio


I've had problems closing my frame, when there was a Timer running.

Here's how I handled it:

def on_close(event):
    timer.Stop()
    frame.Destroy()

wx.EVT_CLOSE(frame, on_close)
Community
  • 1
  • 1
Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
0

I don't work with wxPython, but if there is a method called refresh or something alike, you could start a Thread calling that method every minute.

from threading import Thread
from time import sleep

def refreshApp(app, timespan):
    while app.isRunning:
        app.refresh()
        sleep(timespan)

refresher = Thread(target=worker, args=(myAppInstance, 60))
refresher.start()

EDIT: fixed code so it fits into PEP8

Niklas R
  • 16,299
  • 28
  • 108
  • 203
  • Do you mean my coding style ? – Niklas R Aug 03 '11 at 09:22
  • It's PEP8 not to add extra spaces to align things like that and to do `target=worker` without spaces. I'm sure it's the alignment spacing @MattJ was referring to. Also, generally people only use a trailing comma when necessary. He also might have disliked that you used non-descriptive names like `t` and `worker` or used `60.` when `60` would have been fine. – agf Aug 03 '11 at 13:41
  • IMHO intending the imports like I did is more clear to read. I agree with that `60`. And doing `target = worker` is more clear too, i think. But i will fix the code so it fits in PEP8 – Niklas R Aug 03 '11 at 13:49
0

As Niklas suggested I think you're looking for the Refresh() method: http://wxpython.org/docs/api/wx.Window-class.html#Refresh .

ChrisC
  • 1,282
  • 9
  • 9
  • `Refresh` just forces a redraw of part of a window. Something still has to call `Refresh` "once in a while"... – Mike DeSimone Aug 03 '11 at 21:56
  • Yep, and your post covers that part nicely. wx.Timer's definitely the way to go here. – ChrisC Aug 04 '11 at 02:18
  • got it, but somehow the Refresh() method doesn't work with wx.Frame... no idea why, it's definitely looping every few seconds (printed something to make sure) but Refresh() isn't working :\ – jewz Aug 04 '11 at 07:10
  • Does anything change if you add a call to the Frame's Update() method right after the call to Refresh()? – ChrisC Aug 04 '11 at 13:44