就不多介绍了,直接上代码吧。
#coding:utf-8
import datetime, time, os, wx
import tkinter, threading
from tkinter import * #控件基础包
#全局变量
stopFlag = False;
winWidth = 0;
winHeight = 0;
#倒计时
def countdown(flag):
value_s = var_s.get();
try:
seconds = int(value_s);
#操作
if flag == "shut":
shutdown(value_s);
else:
restart(value_s);
#显示倒计时
show_countdown(seconds);
except:
label_pro.config(text="请输入正确的数字。");
root.update();
return;
#显示倒计时
def show_countdown(seconds):
global stopFlag;
stopFlag = False;
while seconds > 0:
if stopFlag:
break;
time.sleep(1);
seconds = seconds - 1;
label_pro.config(text="关机倒计时: "+str(seconds)+" 秒");
root.update();
#关机操作
def shutdown(seconds):
os.system("shutdown -s -f -t "+seconds);
#重启操作
def restart(seconds):
os.system("shutdown -r -f -t "+seconds);
#终止关机操作
def stopdown():
global stopFlag;
stopFlag = True;
os.system("shutdown -a");
#退出操作
def exit():
still.set(0);
thread.join(0.2);
root.destroy();#关闭窗口
#获取当前时间
def getDateTime():
while still.get()==1:
dateTime = datetime.datetime.now();
hourStr = addZero(dateTime.hour);
minuteStr = addZero(dateTime.minute);
secondStr = addZero(dateTime.second);
timeValue = hourStr+':'+minuteStr+':'+secondStr;
label_time.config(text='当前时间:'+timeValue); #显示当前时间
time.sleep(0.2);
#补零
def addZero(number):
valueStr = str(number);
if number < 10:
valueStr = "0"+valueStr;
return valueStr;
#获取屏幕分辨率
def getScreenResolution():
app = wx.App();
screenArr = wx.DisplaySize()
app.MainLoop();
return screenArr;
#设置软件透明度
def setScreenTransparent(ev=None):
scaleValue = scale.get();
root.attributes('-alpha',scaleValue/100);#半透明
if __name__ == '__main__':
#屏幕分辨率
screenArr = getScreenResolution();
winWidth = screenArr[0];
winHeight = screenArr[1];
###grid布局###
root = tkinter.Tk();
root.title("定时关机");
root.geometry('200x160+'+str(winWidth-230)+'+'+str(winHeight-210));#初始大小与位置
root.attributes('-topmost',1);#总是在顶端
root.overrideredirect(True);#不显示标题栏
#透明度
fm_scale = Frame(root);
fm_scale.grid(row=0, column=0, padx=10, pady=5);
label_t = Label(fm_scale,text="透明度:");
label_t.grid(row=0, column=0);
scale = Scale(fm_scale,width=13,length=120,showvalue=0,from_=20,to=100,resolution=1,orient=HORIZONTAL,command=setScreenTransparent);#缩放比例尺
scale.set(70);#初始值
scale.grid(row=0, column=1);
#当前时间
fm_time = Frame(root);
fm_time.grid(row=1, column=0, padx=10, pady=0);
#窗口是否仍在运行
still = tkinter.IntVar(value=1);
#显示当前时间
label_time = Label(fm_time,text="",foreground='blue',font='Helvetica -14 bold');
label_time.grid(row=0, column=0);
thread = threading.Thread(target=getDateTime);
thread.daemon =True;
thread.start();
#关机时间
fm_show = Frame(root);
fm_show.grid(row=2, column=0, padx=10, pady=5);
label_s = Label(fm_show,text="定时关机:");
label_s.grid(row=0, column=0);
var_s = tkinter.StringVar();
var_s.set(0);
entry_s = Entry(fm_show, textvariable=var_s, width=5);
entry_s.grid(row=0, column=1);
label_s = Label(fm_show,text="秒");
label_s.grid(row=0, column=2);
#提示
fm_pro = Frame(root);
fm_pro.grid(row=3, column=0, padx=10, pady=5);
label_pro = Label(fm_pro,foreground='red',text="关机倒计时: 0 秒",font='Helvetica -14 bold');
label_pro.grid(row=0, column=2);
#按钮操作
fm_but = Frame(root);
fm_but.grid(row=4, column=0, padx=10, pady=0);
but_shut = Button(fm_but,text="关机",width=5,command=lambda:countdown(flag='shut'));
but_shut.grid(row=0, column=3);
but_shut = Button(fm_but,text="重启",width=5,command=lambda:countdown(flag='rest'));
but_shut.grid(row=0, column=4);
but_shut = Button(fm_but,text="终止",width=5,command=stopdown);
but_shut.grid(row=0, column=5);
but_shut = Button(fm_but,text="退出",width=5,command=exit);
but_shut.grid(row=0, column=6);
root.mainloop();