定时关机应用V2.1

2024-01-20 13:04
文章标签 应用 定时 关机 v2.1

本文主要是介绍定时关机应用V2.1,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

# 在ShutDown_2.0的基础上,作了如下改进:
# 1) 修正了默认模式无法选择其他时间的bug,还增加了2.5小时和3小时两个选项;
# 2)自定义模式将计时单位从“秒”改为“分钟”,倒计时显示也优化为“小时:分钟:秒”;
# 3)增加了第三种“定时模式”'''

# 在ShutDown_2.0的基础上,作了如下改进:
# 1)默认模式增加了2.5小时和3小时“两个选项;
# 2)自定义模式将计时单位从“秒”改为“分钟”,倒计时显示也优化为“小时:分钟:秒”;
# 3)增加了第三种“定时模式”'''import datetime
import tkinter as tk
from tkinter import ttk
from threading import Thread
import time
import osdef selected_time(selected_value):match selected_value:case '0.5小时':return 1800case '1小时':return 3600case '1.5小时':return 5400case '2小时':return 7200case '2.5小时':return 9000case '3小时':return 10800class ShutdownApp:def __init__(self, root):self.time_left = 0self.time_left2 = 0self.root = rootself.root.title("定时关机应用V2.1")self.root.resizable(0, 0)screenwidth = self.root.winfo_screenwidth()screenheight = self.root.winfo_screenheight()width = 600height = 200size_geo = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)self.root.geometry(size_geo)self.root.iconphoto(False, tk.PhotoImage(file="C:\\Users\\wokao\\Documents\\MyPythonCode\\ShutDown_v2.0\\""icon.png"))self.root["background"] = "#8DB6CD"self.notebook = tk.ttk.Notebook(self.root)self.framework1 = tk.Frame()self.framework2 = tk.Frame()self.framework3 = tk.Frame()self.notebook.add(self.framework1, text='默认模式')self.notebook.add(self.framework2, text='自定义模式')self.notebook.add(self.framework3, text='定时模式')self.notebook.pack(padx=10, pady=5, fill=tk.BOTH, expand=True)# 选项卡1的界面tk.Label(self.framework1, text="选择倒计时关机时长:").pack()self.cbox = ttk.Combobox(self.framework1)self.cbox['value'] = ('0.5小时', '1小时', '1.5小时', '2小时', '2.5小时', '3小时')self.cbox.current(1)self.selected_value = self.cbox.get()self.cbox.pack()self.start_button = tk.Button(self.framework1, text="开始", command=self.start_timer)self.start_button.pack()self.cancel_button = tk.Button(self.framework1, text="取消关机", state='disabled', command=self.cancel_timer)self.cancel_button.pack()self.timer_label = tk.Label(self.framework1, text="", bg="#8DB6CD")self.timer_label.pack()# 选项卡2的界面tk.Label(self.framework2, text="输入倒计时关机时长(分钟):").pack()self.time_entry2 = tk.Entry(self.framework2)self.time_entry2.pack()self.start_button2 = tk.Button(self.framework2, text="开始", command=self.start_timer2)self.start_button2.pack()self.cancel_button2 = tk.Button(self.framework2, text="取消关机", state='disabled', command=self.cancel_timer2)self.cancel_button2.pack()self.timer_label2 = tk.Label(self.framework2, text="", bg="#8DB6CD")self.timer_label2.pack()self.timer_thread = Noneself.running = False# 选项卡3的界面self.Label3_text = tk.Label(self.framework3, text='现在时间:')self.Label3_text.grid(padx=100, row=0, column=0)self.Label3 = tk.Label(self.framework3,text=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),font=("黑体", 15), fg="white", bg="#8DB6CD")self.Label3.grid(row=0, column=3)self.Label3.after(1000, self.display_timer3)self.l1 = tk.Label(self.framework3, text="小时")self.l1.grid(row=1, column=0)self.input_hour = tk.Entry(self.framework3, bd=0, width=10)self.input_hour.grid(row=1, column=3)self.l2 = tk.Label(self.framework3, text="分钟")self.l2.grid(row=2, column=0)self.input_minute = tk.Entry(self.framework3, bd=0, width=10)self.input_minute.grid(row=2, column=3)self.start_button3 = tk.Button(self.framework3, text="确定", command=self.start_timer3)self.start_button3.grid(row=3, column=0)self.cancel_button3 = tk.Button(self.framework3, text="取消", command=self.cancel_timer3)self.cancel_button3.grid(row=3, column=3)# 选项卡1的功能实现def start_timer(self):try:self.time_left = selected_time(self.cbox.get())except ValueError:self.timer_label.config(text="请选择关机倒计时时长!")returnself.notebook.tab(1, state='disabled')self.notebook.tab(2, state='disabled')self.running = Trueself.start_button.config(state='disabled')self.cancel_button.config(state='normal')self.timer_thread = Thread(target=self.run_timer)self.timer_thread.start()def run_timer(self):timer = datetime.timedelta(seconds=self.time_left)while timer > datetime.timedelta(seconds=0) and self.running:self.timer_label.config(text=f"关机倒计时: {str(timer)} ", font=("黑体", 45), fg="white", bg="#8DB6CD")time.sleep(1)timer -= datetime.timedelta(seconds=1)self.timer_label.config(text="")if self.running:os.system("shutdown /s /t 1")  # 在Windows上执行关机命令def cancel_timer(self):self.running = Falseself.start_button.config(state='normal')self.cancel_button.config(state='disabled')self.timer_label.config(text="已取消关机")self.notebook.tab(1, state='normal')self.notebook.tab(2, state='normal')# 选项卡2的功能实现def start_timer2(self):try:self.time_left2 = int(self.time_entry2.get())except ValueError:self.timer_label2.config(text="请输入有效的数字!")returnself.notebook.tab(0, state='disabled')self.notebook.tab(2, state='disabled')self.running = Trueself.start_button2.config(state='disabled')self.cancel_button2.config(state='normal')self.timer_thread = Thread(target=self.run_timer2)self.timer_thread.start()def run_timer2(self):timer = datetime.timedelta(seconds=60 * self.time_left2)while timer > datetime.timedelta(seconds=0) and self.running:self.timer_label2.config(text=f"关机倒计时: {str(timer)} ", font=("黑体", 45), fg="white", bg="#8DB6CD")time.sleep(1)timer -= datetime.timedelta(seconds=1)self.timer_label2.config(text="")if self.running:os.system("shutdown /s /t 1")  # 在Windows上执行关机命令def cancel_timer2(self):self.running = Falseself.start_button2.config(state='normal')self.cancel_button2.config(state='disabled')self.timer_label2.config(text="已取消关机")self.notebook.tab(0, state='normal')self.notebook.tab(2, state='normal')# 选项卡3的功能实现def display_timer3(self):currentTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))self.Label3.config(text=currentTime)self.root.update()self.Label3.after(1000, self.display_timer3)def start_timer3(self):try:hour = int(self.input_hour.get())minute = int(self.input_minute.get())except ValueError:returncur_time = datetime.datetime.now()cur_time_hour = cur_time.hourcur_time_minute = cur_time.minutehours = ((hour + (minute / 60)) - (cur_time_hour + cur_time_minute / 60))seconds = hours * 60 * 60os.system('shutdown -s -t %d' % seconds)self.notebook.tab(0, state='disabled')self.notebook.tab(1, state='disabled')self.start_button3.config(state='disabled')self.cancel_button3.config(state='normal')def cancel_timer3(self):self.start_button3.config(state='normal')self.cancel_button3.config(state='disabled')self.notebook.tab(0, state='normal')self.notebook.tab(1, state='normal')try:os.system("shutdown -a")except Exception:returnif __name__ == "__main__":ui = tk.Tk()app = ShutdownApp(ui)ui.mainloop()

这篇关于定时关机应用V2.1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/626093

相关文章

Redis中Stream详解及应用小结

《Redis中Stream详解及应用小结》RedisStreams是Redis5.0引入的新功能,提供了一种类似于传统消息队列的机制,但具有更高的灵活性和可扩展性,本文给大家介绍Redis中Strea... 目录1. Redis Stream 概述2. Redis Stream 的基本操作2.1. XADD

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

在Golang中实现定时任务的几种高效方法

《在Golang中实现定时任务的几种高效方法》本文将详细介绍在Golang中实现定时任务的几种高效方法,包括time包中的Ticker和Timer、第三方库cron的使用,以及基于channel和go... 目录背景介绍目的和范围预期读者文档结构概述术语表核心概念与联系故事引入核心概念解释核心概念之间的关系

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关