Xupeng's blog

圆外之大,心向往之

莫非at不能定时执行图形程序?

想趁晚上没有人使用网路时用amule下载一些东西,却发现使用at并不能定时执行图形化的程序,而定时执行console程序是没有问题的,man了一下又google了半天没有任何收获,只得用python临时写一个简单的先凑合用着。
#!/usr/bin/python
#coding=UTF-8

import wx
import os

class MyApp(wx.App):
    def OnInit(self):
        self.frame = wx.Frame(None,-1,'RunAt',size=(290,77))

        self.lblCmd = wx.StaticText(self.frame,-1,'命令:')
        self.txtCmd = wx.TextCtrl(self.frame,-1,size=(150,-1))
        self.btnSelect = wx.Button(self.frame,-1,'选择')
        self.btnSelect.Bind(wx.EVT_BUTTON,self.Select)

        self.lblTime = wx.StaticText(self.frame,-1,'时间:')
        self.txtTime = wx.TextCtrl(self.frame,-1,size=(150,-1))
        self.btnSet = wx.Button(self.frame,-1,'设定')
        self.btnSet.Bind(wx.EVT_BUTTON,self.Set)

        sizer1 = wx.BoxSizer(wx.HORIZONTAL)
        sizer1.Add(self.lblCmd,0,wx.LEFT|wx.TOP|wx.ALIGN_CENTER,5)
        sizer1.Add(self.txtCmd,0,wx.LEFT|wx.TOP|wx.ALIGN_CENTER,5)
        sizer1.Add(self.btnSelect,0,wx.LEFT|wx.TOP|wx.ALIGN_CENTER,5)

        sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer2.Add(self.lblTime,0,wx.LEFT|wx.TOP|wx.ALIGN_CENTER,5)
        sizer2.Add(self.txtTime,0,wx.LEFT|wx.TOP|wx.ALIGN_CENTER,5)
        sizer2.Add(self.btnSet,0,wx.LEFT|wx.TOP|wx.ALIGN_CENTER,5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(sizer1,0)
        sizer.Add(sizer2,0)

        self.frame.SetSizer(sizer)

        self.runTime = None

        self.timer = wx.Timer()
        self.timer.Bind(wx.EVT_TIMER,self.OnTimer)
        self.timer.Start()

        self.frame.Bind(wx.EVT_ICONIZE,self.OnMin)

        self.frame.Center()
        self.frame.Show(True)
        self.SetTopWindow(self.frame)

        return True

    def Select(self,event):
        dlg = wx.FileDialog(self.frame)
        if dlg.ShowModal() == wx.ID_OK:
            self.txtCmd.SetValue(dlg.GetPath())

    def OnTimer(self,event):
        if self.runTime == None:
            pass
        t = wx.DateTime.Now()
        str = '%02d:%02d' %(t.GetHour(),t.GetMinute())
        if str == self.runTime and t.GetSecond() == 0:
            os.system('%s&' %self.txtCmd.GetValue())

    def Set(self,event):
        self.runTime = self.txtTime.GetValue()

    def OnMin(self,event):
        self.frame.Show(False)

def main():
    app = MyApp(0)
    app.MainLoop()

if __name__ == '__main__':
    main()

Comments