I am new to Python and I'm trying to make a simple updating window. Most suggest I should use wxtimer for the task. I have tried to get it going but it fails. Here is the code without a timer that works for me. I had been using time.sleep in my working terminal version. I've looked over several examples, but nothing seems to work on my system. You can see what I am trying to do by looking at the stuff that is commented out. Can anybody help?

import wx
from get_last_google import last_price

class EnterData(wx.Frame):
    
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, 'Data Entry', size=(400, 100))
                
        panel = wx.Panel(self, -1)
        
        #self.timer = wx.timer(self)
        #self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
        #self.timer.start(1000)
           
        text = wx.TextEntryDialog(None, "Enter ticker:", 'Real-Time Ticker', '<Enter Symbol>')
        if text.ShowModal()==wx.ID_OK:
            symbol=text.GetValue()
                
        self.Show(True)  
            
    #def on_timer(self, event):
        a=last_price("",symbol)
        wx.StaticText(panel, -1, a.symbol+" "+str(a.Last)+" "+a.MDY, (10,10))   # this was different...
        #Could be anything really

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = EnterData().Show()
    app.MainLoop()

Here is the error I get...

Traceback (most recent call last):
  File "/Stocks/menu1.py", line 74, in <module>
    frame = EnterData().Show()
  File "/Stocks/menu1.py", line 38, in __init__
    self.timer = wx.timer(self)
AttributeError: 'module' object has no attribute 'timer'

You have to use T not t
self.timer = wx.Timer(self)

Thanks! I'll give it a try.

I have changed the capitalization, and made some other improvements (I think...), but I still can's get StaticText to update the values. The values update in the terminal window using print, but no luck with the GUI. Only the first value shows up. Here is where I am at now. You can see I am having trouble passing variables in a more local manner as well. A good bit of redundancy here...btw running python 2.7.2...

import wx
import time
from get_last_google import last_price

class EnterData(wx.Frame):
    
    def __init__(self, parent, id):
        wx.Frame.__init__(self, None, wx.ID_OK, 'Data Entry', size=(400, 100))
        
        panel = wx.Panel(self, wx.ID_OK)
        global locSymbol
        text = wx.TextEntryDialog(None, "Enter ticker:", 'Real-Time Ticker', '<Enter Symbol>')
        if text.ShowModal()==wx.ID_OK:
            locSymbol=text.GetValue()
        
        self.Timer = wx.Timer(self, wx.ID_OK)
        self.Timer.Start(1000)
        self.Bind(wx.EVT_TIMER, self.on_timer, self.Timer)
    
        a=last_price("",locSymbol)      
        locLast = str(a.Last)
        locMDY = a.MDY        
        wx.StaticText(panel, -1, locSymbol+" "+locLast+" "+locMDY, (10,10))        
        
        self.Show(True)  
            
    def on_timer(self, event):
        a=last_price("", locSymbol)
        locLast = str(a.Last)
        locMDY = a.MDY  
#print locMDY
        
if __name__ == "__main__":
    app = wx.App()
    frame = EnterData(None, -1)
    frame.Show = True
    app.SetTopWindow(frame)
    app.MainLoop()

OK. Someone in the wxPython-user groups figured it out for me. Thanks for the help anyway though. It seems I wasn't really calling the timer. They also helped me get that global declaration out. Basically, it seems I use self every time (whether inside of __init__, or outside of __init__) for variables I need outside of the __init__ function. This is solved as far as I'm concerned, but I welcome any suggestions anyone may have in improving the code. Here's the working code...

import wx
import time
from get_last_google import last_price

class EnterData(wx.Frame):
    
    def __init__(self, parent, id):
        wx.Frame.__init__(self, None, wx.ID_OK, 'Real-Time Ticker', size=(400, 100))
        
        panel = wx.Panel(self, wx.ID_OK)

        self.text = wx.TextEntryDialog(None, "Enter ticker:", 'Real-Time Ticker', '<Enter Symbol>')
        if self.text.ShowModal()==wx.ID_OK: 
            self.locSymbol=self.text.GetValue() 
        
        self.Timer = wx.Timer(self, wx.ID_OK)
        self.Timer.Start(1000)
        self.Bind(wx.EVT_TIMER, self.on_timer, self.Timer)
    
        a=last_price("",self.locSymbol)      
        locLast = str(a.Last)
        locMDY = a.MDY        
        
        self.stText = wx.StaticText(panel, -1, self.locSymbol+" "+locLast+" "+locMDY, (10,10))      
        
        self.Show(True)  
            
    def on_timer(self, event):
        a=last_price("", self.locSymbol)
        locLast = str(a.Last)
        locMDY = a.MDY  

        self.stText.SetLabel(self.locSymbol + " " + locLast + " " + locMDY) 
        
if __name__ == "__main__":
    app = wx.App()
    frame = EnterData(None, -1)
    frame.Show = True
    app.SetTopWindow(frame)
    app.MainLoop()
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.