I wrote a GUI tool that will display stats and refresh the stats when a refresh button is pushed. Now I would like to add a feature for the stats to auto refresh every five minutes. Is it possible to have the UI updated without binding an event handler?

Thanks in advance

Mece

Hmmm, well the only way that I can think of off the top of my head would be to create a thread that will replace the event timer. that thread would then execute a loop that will sleep for a period of time then refresh the panel when it wakes up. This works in theory, but I have had no experience doing so.

I'm curious though why you don't want to use an event...

Happy Coding!

You can use wx.Timer to bind to self to do that job.

eg.

import wx

class Frame(wx.Frame):
    """
    #Class stuff decl
    """
    def __init__(self,parent):
        wx.Frame.__init__(self,parent)
        self.timer=wx.Timer(self)
        self.count=0
       
        #timer event Always must be bound to self
        self.Bind(wx.EVT_TIMER,self.evt_timer)
        self.Bind(wx.EVT_PAINT,self.paint)
        self.Show()
        ############### event  methods ###########
    def paint(self,event):
        self.timer.Start(100)# increase the value for more time
            
    def evt_timer(self,event):
        self.count +=1
        if self.count== 20:
            # Do your more stuff here
            print ("hello world")  
            self.count=0 # reset the count
            
if __name__ =="__main__":
    app=wx.App()
    Frame(None)
    app.MainLoop()

This should help you for now......
;)

Hmmm, well the only way that I can think of off the top of my head would be to create a thread that will replace the event timer. that thread would then execute a loop that will sleep for a period of time then refresh the panel when it wakes up. This works in theory, but I have had no experience doing so.

I'm curious though why you don't want to use an event...

Happy Coding!

I am new to wxPython, I was thinking of adding a check box that when selected would use loop the update process. But first was wanted to try having the stats update automatically. Thanks for the response, I will give it a try.

def __init__(self,parent,title):
	self.timer = wx.Timer(self)
	self.Bind(wx.EVT_TIMER,self.OnRefresh)
	self.timer.Start(600000) #10 minutes

def OnRefresh(self,event):
	statusBar = self.GetStatusBar()
        qtotals = self.GetQTotals()

        #Update Totals
        self.sat1TotalLabel.SetLabel(qtotals.get('stat1'))
        self.stat2TotalLabel.SetLabel(qtotals.get('stat2'))
        self.stat3TotalLabel.SetLabel(qtotals.get('stat3'))
        self.stat4TotalLabel.SetLabel(qtotals.get('stat4'))
        self.stat5TotalLabel.SetLabel(qtotals.get('stat5'))
        
        time = self.CurrentTime()
        statusMsg = "Updated: "+time
        statusBar.SetStatusText(statusMsg)
        statusBar = statusBar.SetFont(self.smallFont)

Thanks for the suggestion. The final solution was a little different. The auto refresh occurred every 10 minutes and calls the same function as the refresh button event which changes the text on the labels.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.