定时器的使用

主要介绍下 时间的定时, 还有计数的定时

#!/usr/bin/python

#-*- coding:utf8 -*-

import wx
import time


class MyFrame(wx.Frame):
    def __init__(self, root):
        wx.Frame.__init__(self, root, -1, u"定时器示例", size=(400,400))
        panel = wx.Panel(self, -1)
        #self.count = 0
        #self.timer = wx.PyTimer(self.Notify)
        #self.timer.Start(1000)
        
        self.inputText = wx.TextCtrl(panel, -1, "", pos=(10,10), size=(200,50))
        self.inputText2 = wx.TextCtrl(panel, -1, "", pos=(20,10), size=(200,50))


        self.count = 0
        self.timer = wx.PyTimer(self.Notify)
        self.timer.Start(1000)

        self.startBtn = wx.Button(panel, -1, u"开始")
        self.endBtn = wx.Button(panel, -1, u"停止")

        self.Bind(wx.EVT_BUTTON, self.OnStart, self.startBtn)
        self.Bind(wx.EVT_BUTTON, self.OnEnd, self.endBtn)
        
        sizer = wx.FlexGridSizer(cols=1, hgap=10, vgap=10)
        sizer.Add(self.inputText)
        sizer.Add(self.inputText2)
        sizer.Add(self.startBtn)
        sizer.Add(self.endBtn)
        panel.SetSizer(sizer)
        panel.Fit()

    def OnStart(self, event):
        self.timer2 = wx.CallLater(1000, self.OnCallTimer, 1,2,3)
        
    def OnEnd(self, event):
        self.timer2.Stop()
        self.inputText2.Value = str(self.count)
        del self.timer2
        
    def OnCallTimer(self, *args, **kw):
        self.count = self.count+1
        self.inputText2.Value = str(self.count)
        tup = args
        total = 0
        for x in tup:
            total = total + int(x)
        self.timer2.Restart(1000, total)
        return total

    def Notify(self):
        t = time.localtime(time.time())
        self.inputText.Value = time.strftime("%d-%B-%Y %I:%M:%S", t)

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None)
        self.frame.Show()
        return True

app = MyApp()
app.MainLoop()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值