Thank you Robin, actually, I’ve looked through the document several times. anyway, As a newbie, the problem are not solved. Take a look at the code directly:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class WorkerThread(Thread):
def __init__(self, Delta, window):
Thread.__init__(self)
self.Delta = Delta
self.window = window
self.timeToQuit = threading.Event()
def stop(self):
self.timeToQuit.set()
def run(self):
tmNow = datetime.now()
timedDelta = (self.Delta - tmNow).total_seconds()
timedDelta = int(timedDelta)
if timedDelta>0:
os.system('shutdown -s -f -t %s' %timedDelta)
while True:
tmNow = datetime.now()
timedDelta = (self.Delta - tmNow).total_seconds()
if timedDelta>0 and not self.timeToQuit.isSet():
sec = str(timedDelta)
self.window.showMe.ChangeValue('关机倒计时:%s秒' %sec)
time.sleep(1)
else:
break
self.timeToQuit.clear()
else:
self.window.showMe.ChangeValue('时间不可倒流,请重新设置时间')
self.window.calBtn.Enable()
self.timeToQuit.clear()
class MainWindow(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="自动关机", pos=(512,225),
size=(300,270), style=wx.DEFAULT_FRAME_STYLE^wx.RESIZE_BORDER^wx.MAXIMIZE_BOX)
self.panel = wx.Panel(self, wx.ID_ANY)
labelOne = wx.StaticText(self.panel, wx.ID_ANY, "关机日期: ")
self.inputTxtOne = wx.adv.DatePickerCtrl(self.panel, -1, style=wx.adv.DP_DEFAULT|wx.adv.DP_SHOWCENTURY)
labelTwo = wx.StaticText(self.panel, wx.ID_ANY, "关机时间: ")
self.inputTxtTwo = wx.adv.TimePickerCtrl(self.panel, -1)
self.showMe = wx.TextCtrl(self.panel, wx.ID_ANY, size=(500,50),
style=wx.TE_MULTILINE | wx.TE_READONLY)
self.calBtn = wx.Button(self.panel, wx.ID_ANY, '启动关机程序')
clBtn = wx.Button(self.panel, wx.ID_ANY, '取消关机')
self.Bind(wx.EVT_BUTTON, self.ShutDown, self.calBtn)
self.Bind(wx.EVT_BUTTON, self.Canl, clBtn)
self.CreateStatusBar()
self.SetStatusText('最后跟新日期:2019-9-2 QQ:444849151')
topSizer = wx.BoxSizer(wx.VERTICAL)
OneSizer = wx.BoxSizer(wx.HORIZONTAL)
TwoSizer = wx.BoxSizer(wx.HORIZONTAL)
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
OneSizer.Add(labelOne, 0, wx.ALL, 5)
OneSizer.Add(self.inputTxtOne, 1, wx.ALL|wx.EXPAND, 5)
TwoSizer.Add(labelTwo, 0, wx.ALL, 5)
TwoSizer.Add(self.inputTxtTwo, 1, wx.ALL|wx.EXPAND, 5)
btnSizer.Add(self.calBtn, 0, wx.ALL, 5)
btnSizer.Add(clBtn, 0, wx.ALL, 5)
topSizer.Add(OneSizer, 0, wx.ALL|wx.EXPAND, 5)
topSizer.Add(TwoSizer, 0, wx.ALL|wx.EXPAND, 5)
topSizer.Add(self.showMe, 0, wx.ALL|wx.EXPAND, 5)
topSizer.Add(btnSizer, 0, wx.ALL|wx.CENTER, 5)
self.panel.SetSizerAndFit(topSizer) #Layout sizers
self.threads = []
def ShutDown(self, e):
self.calBtn.Disable()
self.showMe.Clear()
dx = self.inputTxtOne.GetValue()
dt = wx.wxdate2pydate(dx)
d = datetime.date(dt)
tx = self.inputTxtTwo.GetValue()
tt = wx.wxdate2pydate(self.tx)
t = datetime.time(tt)
shtdownTime = datetime.combine(d,t)
thread = WorkerThread(shtdownTime, self)
self.threads.append(thread)
thread.start()
def Canl(self, e):
for i in range(len(self.threads)):
thread = self.threads[i]
thread.stop()
self.calBtn.Enable()
os.system('shutdown -a')
self.showMe.SetValue('已取消关机')
app = wx.App(None)
frame = MainWindow().Show()
app.MainLoop()
I want to set the default time to 23:00 for the day. The location is in MainWindow():self.inputTxtTwo = wx.adv.TimePickerCtrl(self.panel, -1, dt=???)