闲来无事,给老妈写了一个开心网收菜倒计时工具,有兴趣的可以看看。
工具使用了wxpython和configobj,请到相应网站自行下载。先附上一张截图。
下面是python代码以及配置文件和图标。
python代码(iTimer.py):


# -*- coding: utf-8 -*-
# Author: blade
# E-mail: flyblade@gmail.com
import wx
import os, sys
from configobj import ConfigObj as co
from datetime import datetime, timedelta
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.parent = parent
self.readCfg()
self.timeTextCtrl = wx.TextCtrl(self, -1, "10")
h = self.timeTextCtrl.GetSize().height
w = self.timeTextCtrl.GetSize().width + \
self.timeTextCtrl.GetPosition().x + 2
self.spin = wx.SpinButton(self, -1, (w, 50),
(h*2/3, h), wx.SP_VERTICAL)
self.spin.SetRange(1, 999)
self.spin.SetValue(10)
self.Bind(wx.EVT_SPIN, self.OnSpin, self.spin)
self.whoComboBox = wx.ComboBox(self, -1, self.who[0],
(15, 30), wx.DefaultSize, self.who, wx.CB_DROPDOWN)
self.whatComboBox = wx.ComboBox(self, -1, self.what[0],
(15, 30), wx.DefaultSize, self.what, wx.CB_DROPDOWN)
self.startBtn = wx.Button(self, -1, u"开始计时")
self.startBtn.SetDefault()
self.stopBtn = wx.Button(self, -1, u"停止计时")
self.newBtn = wx.Button(self, -1, u"新计时器")
self.delBtn = wx.Button(self, -1, u"删除计时器")
self.stopBtn.Disable()
self.timer = wx.Timer(self)
# Bind the event
self.Bind(wx.EVT_BUTTON, self.OnClick, self.startBtn)
self.Bind(wx.EVT_BUTTON, self.OnClick, self.stopBtn)
self.Bind(wx.EVT_BUTTON, self.OnClick, self.newBtn)
self.Bind(wx.EVT_BUTTON, self.OnClick, self.delBtn)
self.Bind(wx.EVT_TIMER, self.OnTimerUpdate)
self._layout()
def _layout(self):
box = wx.StaticBox(self, -1, "")
sizer = wx.StaticBoxSizer(box, wx.HORIZONTAL)
self.timeTextCtrl.SetInitialSize((40, -1))
sizer.Add(self.timeTextCtrl)
sizer.Add(self.spin)
sizer.Add(wx.StaticText(self, label=u" 分钟后去 "),
flag = wx.ALIGN_CENTER)
sizer.Add(self.whoComboBox)
sizer.Add(wx.StaticText(self, label=u" 家 "),
flag = wx.ALIGN_CENTER)
sizer.Add(self.whatComboBox, 1)
sizer.Add(self.startBtn)
sizer.Add(self.stopBtn)
sizer.Add(self.newBtn)
sizer.Add(self.delBtn)
self.SetSizer(sizer)
def OnSpin(self, event):
self.timeTextCtrl.SetValue(str(event.GetPosition()))
def OnClick(self, event):
id = event.GetId()
if id == self.startBtn.GetId():
who = self.whoComboBox.GetValue()
what = self.whatComboBox.GetValue()
duration = int(self.timeTextCtrl.GetValue())
self.t = wx.CallLater(duration*1000*60,
self.Alarm,
u"快去" + who + u"家" + what + u"!")
needWrite = False
if who not in self.who:
self.who.insert(0, who)
needWrite = True
if what not in self.what:
self.what.insert(0, what)
needWrite = True
if needWrite:
self.writeCfg()
self.startBtn.Disable()
self.delBtn.Disable()
self.stopBtn.Enable()
self.timeTextCtrl.Disable()
self.spin.Disable()
self.whoComboBox.Disable()
self.whatComboBox.Disable()
# some time
self.startDateTime = datetime.now()
self.stopDateTime = self.startDateTime + \
timedelta(minutes=duration)
self.timer.Start(1000)
elif id == self.stopBtn.GetId():
self._stop()
elif id == self.newBtn.GetId():
self.parent.addPanel()
elif id == self.delBtn.GetId():
if self.parent.panel_number == 1:
dlg = wx.MessageDialog(self,
u"还删?都没了!", u"不能删除",
wx.OK | wx.ICON_WARNING )
dlg.ShowModal()
dlg.Destroy()
else:
self.parent.delPanel(self)
def _stop(self):
self.startBtn.SetLabel(u"开始计时")
self.startBtn.Enable()
self.delBtn.Enable()
self.stopBtn.Disable()
self.timeTextCtrl.Enable()
self.spin.Enable()
self.whoComboBox.Enable()
self.whatComboBox.Enable()
if self.timer:
self.timer.Stop()
if self.t:
self.t.Stop()
self.t = None
def _formatTD(self, td):
s = u""
sec = td.days*24*60*60 + td.seconds
min, sec = divmod(sec, 60)
hrs, min = divmod(min, 60)
if hrs != 0:
s += u"%02d小时" % hrs
if min != 0:
s += u"%02d分钟" % min
if sec != 0:
s += u"%02d秒" % sec
return s
def OnTimerUpdate(self, event):
td = self.stopDateTime - datetime.now()
self.startBtn.SetLabel(self._formatTD(td))
def Alarm(self, msg):
dlg = wx.MessageDialog(self, msg, u"时间到了",
wx.OK | wx.ICON_WARNING | wx.STAY_ON_TOP)
self._stop()
dlg.ShowModal()
dlg.Destroy()
def readCfg(self):
self.config = co("config.ini", encoding='UTF8')
self.who = self.config['who']
self.what = self.config['what']
def writeCfg(self):
config = co(encoding='UTF8')
config.filename = "config.ini"
config['who'] = self.who
config['what'] = self.what
config.write()
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, u"倒计时工具",
style=wx.DEFAULT_FRAME_STYLE & ~wx.RESIZE_BORDER \
& ~wx.MAXIMIZE_BOX)
self.SetIcon(wx.Icon('iTimer.png', wx.BITMAP_TYPE_PNG))
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self.sizer)
self.panel_number = 0
self.addPanel()
def addPanel(self):
self.panel_number += 1
self.sizer.Add(MyPanel(self), 1, wx.EXPAND)
self._setSize()
def delPanel(self, panel):
self.panel_number -= 1
self.sizer.Detach(panel)
panel.Destroy()
self._setSize()
def _setSize(self):
self.SetSize((650, 32 + 40*self.panel_number))
class iTimer(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
topFrame = MyFrame()
self.SetTopWindow(topFrame)
topFrame.CenterOnScreen()
topFrame.Show()
return 1
if __name__=="__main__":
sys.path.append(os.path.dirname(os.path.curdir))
app = iTimer(0)
app.MainLoop()
配置文件(config.ini):
who = 张三, 李四
what = 收小猪, 收鸡蛋, 收牛奶
what = 收小猪, 收鸡蛋, 收牛奶
图标:
可执行文件包由于加入了python和wx的一些dll文件,比较大,如没人需要,就不上传了。