定时器

wxpyton 定时器应用示例

import wx
import wx.adv
import datetime

class MyFrame2(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="定时器开始啦")
        self.Bind(wx.EVT_CLOSE, self.OnClose)

    def OnClose(self, event):
        self.Destroy()
        wx.GetApp().ExitMainLoop()
        event.Skip()

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="请最小化")
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_ICONIZE, self.OnIconify)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
        self.Bind(wx.EVT_CLOSE, self.OnClose)

    def OnClose(self, event):
        self.Destroy()
        event.Skip()

    def OnIconify(self, event):
        if self.IsIconized():
            now = datetime.datetime.now()
            target_time_1 = datetime.datetime(now.year, now.month, now.day, 9, 58, 30)
            target_time_2 = datetime.datetime(now.year, now.month, now.day, 10, 58, 30)
            if now < target_time_1:
                seconds = (target_time_1 - now).total_seconds()
            else:
                seconds = (target_time_2 - now).total_seconds()
            if seconds > 0:
                self.timer.Start(int(seconds * 1000), oneShot=True)
                wx.adv.NotificationMessage( title='提示', message='目标时间剩余%s秒'%seconds, parent=None, flags=wx.ICON_INFORMATION).Show(timeout=1000)
                self.tbicon = MyTaskBarIcon(self)
                self.Hide()
            else:
                wx.adv.NotificationMessage( title='提示', message='已过目标时间', parent=None, flags=wx.ICON_INFORMATION).Show(timeout=1000)
                self.Close()
                wx.GetApp().ExitMainLoop()
        else:
            self.tbicon.RemoveIcon()
            self.tbicon.Destroy()
            self.tbicon = None
            self.Show()
            self.Raise()
    
    def OnTimer(self, event):
        wx.adv.NotificationMessage( title='提示', message='定时器开始', parent=None, flags=wx.ICON_INFORMATION).Show(timeout=1500)
        self.Close()
        frame2 = MyFrame2()
        frame2.Center()
        frame2.Show()
        frame2.Raise()


class MyTaskBarIcon(wx.adv.TaskBarIcon):
    def __init__(self, parent):
        wx.adv.TaskBarIcon.__init__(self)
        self.parent = parent
        icon = wx.Icon('icon.ico', wx.BITMAP_TYPE_ICO)
        self.SetIcon(icon, '定时显示')
        self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.OnTaskBarLeftClick)
        
    def OnTaskBarLeftClick(self, event):
        self.RemoveIcon()
        self.parent.Iconize(False)
        self.parent.Show()
        self.parent.Raise()

app = wx.App()
frame = MyFrame()
frame.Show()
app.MainLoop()

 

posted @ 2024-03-01 12:35  rex4399  阅读(70)  评论(0)    收藏  举报