Wxpython自定义时间微调器

时间:2018-03-01 14:40:39

标签: wxpython wxpython-phoenix

我正在寻找一个时间微调器小部件HH:MM:SS,无限小时跨度。

我可以超过24小时。

我在演示中找到的只有一个在12或24小时范围内有限制。

我想创建一个用户关闭时钟。

1 个答案:

答案 0 :(得分:1)

这是一个简单的倒数计时器 我使用time.time()而不是仅仅增加timer中的点数,因为timer并不特别准确,并且会在很长一段时间内漂移很多。

import wx
import time
class myFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self,*args,**kwds)
        self.SetSize((275,200))
        font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
        font.SetPointSize(20)
        self.panel = wx.Panel(self, wx.ID_ANY)
        label_hh = wx.StaticText(self.panel, wx.ID_ANY, ("Hrs"))
        label_mm = wx.StaticText(self.panel, wx.ID_ANY, ("Mins"))
        label_ss = wx.StaticText(self.panel, wx.ID_ANY, ("Secs"))
        self.time_hh = wx.SpinCtrl(self.panel, wx.ID_ANY,size=(50,20), min=0, max=8760, initial=0 )
        self.time_mm = wx.SpinCtrl(self.panel, wx.ID_ANY,size=(40,20), min=0, max=59, initial=0 )
        self.time_ss = wx.SpinCtrl(self.panel, wx.ID_ANY,size=(40,20), min=0, max=59, initial=0 )
        self.start_button = wx.Button(self.panel, label="Start", size=(40,25))
        self.quit_button = wx.Button(self.panel, label="Quit", size=(40,25))
        ctext = wx.StaticText(self.panel, wx.ID_ANY, "Shutdown Timer")
        self.display = wx.TextCtrl(self.panel, wx.ID_ANY, "",size=(250,50))
        self.display.SetToolTip("Hours,Minutes,Seconds")
        self.display.SetBackgroundColour(wx.GREEN)
        self.display.SetFont(font)
        sizer1 = wx.FlexGridSizer(0,3,5,5)
        sizer1.Add(label_hh, flag = wx.ALL|wx.EXPAND)
        sizer1.Add(label_mm, flag = wx.ALL|wx.EXPAND)
        sizer1.Add(label_ss, flag = wx.ALL|wx.EXPAND)
        sizer1.Add(self.time_hh, flag =wx.ALL|wx.EXPAND)
        sizer1.Add(self.time_mm, flag =wx.ALL|wx.EXPAND)
        sizer1.Add(self.time_ss, flag =wx.ALL|wx.EXPAND)
        sizer1.Add(self.start_button, flag = wx.ALL|wx.CENTER)
        sizer1.Add(self.quit_button, flag = wx.ALL|wx.CENTER)
        mainsizer = wx.BoxSizer(wx.VERTICAL)
        mainsizer.Add(sizer1)
        mainsizer.Add(ctext, flag = wx.ALL|wx.EXPAND)
        mainsizer.Add(self.display, flag = wx.ALL|wx.EXPAND)
        self.panel.SetSizer(mainsizer)
        self.start_button.Bind(wx.EVT_BUTTON, self.OnStart)
        self.quit_button.Bind(wx.EVT_BUTTON, self.OnQuit)
        self.Show()
        self.real_time = 0
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)

    def OnStart(self, event):
        watch = ((self.time_hh.GetValue() * 60) * 60) + (self.time_mm.GetValue() * 60) + self.time_ss.GetValue()
        now = time.time()
        self.real_time = watch + now
        self.timer.Start(1000)

    def OnQuit(self, event):
        self.timer.Stop()
        self.Close()

    def OnTimer(self, event):
        now=time.time()
        watch = self.real_time - now
        if watch < 1:
            self.timer.Stop()
            self.display.SetValue("Shutting down!")
            return
        if watch < 60:
            self.display.SetBackgroundColour(wx.RED)
        m,s = divmod(watch,60)
        mm = m
        h,m = divmod(m,60)
        d,h = divmod(h,24)
        if d < 1:
            self.display.SetValue("%02d:%02d:%02d" % (h,m,s))
        elif d == 1:
            self.display.SetValue("%d Day  %02d:%02d:%02d" % (d,h,m,s))
        else:
            self.display.SetValue("%d Days  %02d:%02d:%02d" % (d,h,m,s))

if __name__ == '__main__':
    app = wx.App()
    myFrame(None)
    app.MainLoop()

enter image description here

enter image description here