之前用wxpython实现了一个模拟时钟的例子,本节LEDNumberCtrl 部件将展示一个数字时钟。
专题:wxpython中文教程
例子,wx.Frame中展示出当前本地时间。
代码:
 
#!/usr/bin/python
#coding=utf-8
#lednumber.py
import wx
import time
import wx.gizmos as gizmos
class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, 
                          wx.DefaultPosition, wx.Size(650, 100))
        self.led = gizmos.LEDNumberCtrl(self, -1, wx.DefaultPosition, 
                                        wx.DefaultSize, gizmos.LED_ALIGN_CENTER)
        self.OnTimer(None)
        self.timer = wx.Timer(self, -1)
        self.timer.Start(1000)
        self.Bind(wx.EVT_TIMER, self.OnTimer)
        self.Center()
        
    def OnTimer(self, event): 
        t = time.localtime(time.time())
        st = time.strftime("%I:%M:%S", t)
        self.led.SetValue(st)
      
class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'lednumber.py')
        frame.Show(True)
        self.SetTopWindow(frame)
        return True
    
app = MyApp(0)
app.MainLoop()
它有以下标志:
 
对于 windows 用户,需要在代码中添加一个 wx.callafter():
 
如图:
