Using wx.CallLater in wxPython

2019-08-22 01:01发布

问题:

I'm trying to make an object that handles basic time related functions. I want it so when any of the attributes (e.g. self.Time ) are referenced they are up to date. However, for some reason wx.CallLater(1000, self.Tick) doesn't seem to be updating the attributes. It will only print self.Time once, as opposed to every second (like I want it to).

How would I go about getting the behavior I desire? Should I use something besides wx.CallLater(1000, self.Tick)?

Snippet :

import wx, re, time, win32api, calendar

class TimeDate :

    def __init__ (self) :

        self.Tick()

    def Tick (self) :

        self.Year     = int(time.strftime("%Y"))
        self.Month    = int(time.strftime("%m"))
        self.Calendar = calendar.month(self.Year, self.Month)
        self.Date     = time.strftime('%A, %B %d, %Y')
        self.Time     = time.strftime('%I:%M %S %p')

        print self.Time

        wx.CallLater(1000, self.Tick)

回答1:

What you want is wx.Timer, not wx.CallAfter or wx.CallLater. See the following tutorial on timers for more information:

http://www.blog.pythonlibrary.org/2009/08/25/wxpython-using-wx-timers/

Both of the those "Call*" methods only fire once.



回答2:

Use wx.CallAfter instead - wx.CallLater is a class and should be used differently.

wx.CallAfter, on the other hand, seems to be exactly what you need.



回答3:

I think both wx.CallAfter and wx.CallLater are called only once.....