2011-08-10 63 views
0

我是wxPython的新手,無法解決一個問題。我需要不斷更新面板的時鐘值。我有一個解決方案,但在這種情況下,我通常無法關閉窗口(alt + f4不起作用)。 另外我不會理解.Update .Refresh和.Destroy應該被調用的區別是什麼?wxPython繼續更新面板

有人可以推薦一本好書,如何在wxPython中編程? 感謝您的幫助。

class TimeDatePanel(wx.Panel): 
def __init__(self, parent, ID=ID_TIMEDATE, pos=wx.DefaultPosition, size=(50, 50), controller=None): 
    wx.Panel.__init__(self, parent, ID, pos, size, wx.RAISED_BORDER) 
    self.controller = controller 
    transCoded = controller.transCodes 
    layout = wx.GridSizer(5,2,0,10) 
    layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("Time & Date"))) 
    layout.Add(wx.StaticText(self, wx.ID_ANY, ""), 0,flag=wx.ALL) 
    layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("Local time")), 0,flag=wx.ALL|wx.ALIGN_RIGHT) 
    self.LT = wx.StaticText(self, wx.ID_ANY, "") 
    layout.Add(self.LT) 
    layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("UTC")), 0,flag=wx.ALL|wx.ALIGN_RIGHT) 
    self.UTC = wx.StaticText(self, wx.ID_ANY, "") 
    layout.Add(self.UTC) 
    layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("Julian day")), 0,flag=wx.ALL|wx.ALIGN_RIGHT) 
    self.JD = wx.StaticText(self, wx.ID_ANY, "") 
    layout.Add(self.JD) 
    layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("Local sidereal time")), 0,flag=wx.ALL|wx.ALIGN_RIGHT) 
    self.LST = wx.StaticText(self, wx.ID_ANY, "") 
    layout.Add(self.LST) 
    self.SetSizer(layout) 
    self.updateTimeDate() 
    self.Fit() 

    wx.EVT_PAINT(self, self.onPaint) 

def onPaint(self, event=None): 
    self.updateTimeDate() 

def updateTimeDate(self): 
    mechanics = self.controller.mechanics 
    self.LT.SetLabel(str(mechanics.getLT())) 
    self.UTC.SetLabel(str(mechanics.getUTC())) 
    self.JD.SetLabel(str(mechanics.getYD())) 
    self.LST.SetLabel(str(mechanics.getLST())) 

回答

2

如果需要更新的每隔一段時間時鐘,爲什麼不使用AnalogClock,LEDNumberCtrl或者也許這就是一個wx.Timer更新TimeCtrl?以下教程將幫助您使用計時器部分:http://www.blog.pythonlibrary.org/2009/08/25/wxpython-using-wx-timers/

前兩個小部件會自行更新。在靜態設置StaticText控件或其他常規窗口小部件的值時,您必須調用Update,Refresh或Layout。只需使用SetValue或SetLabel。

羅賓鄧恩有一本名爲「wxPython in Action」的舊書,它在很大程度上仍然很棒。還有一個由Cody Precord編寫的wxPython Cookbook,今年推出。

+0

感謝您的幫助,它很有用 – ekitru