定时关机应用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

相关文章

Python Flask 库及应用场景

《PythonFlask库及应用场景》Flask是Python生态中​轻量级且高度灵活的Web开发框架,基于WerkzeugWSGI工具库和Jinja2模板引擎构建,下面给大家介绍PythonFl... 目录一、Flask 库简介二、核心组件与架构三、常用函数与核心操作 ​1. 基础应用搭建​2. 路由与参

Spring Boot中的YML配置列表及应用小结

《SpringBoot中的YML配置列表及应用小结》在SpringBoot中使用YAML进行列表的配置不仅简洁明了,还能提高代码的可读性和可维护性,:本文主要介绍SpringBoot中的YML配... 目录YAML列表的基础语法在Spring Boot中的应用从YAML读取列表列表中的复杂对象其他注意事项总

电脑系统Hosts文件原理和应用分享

《电脑系统Hosts文件原理和应用分享》Hosts是一个没有扩展名的系统文件,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应... Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应

CSS 样式表的四种应用方式及css注释的应用小结

《CSS样式表的四种应用方式及css注释的应用小结》:本文主要介绍了CSS样式表的四种应用方式及css注释的应用小结,本文通过实例代码给大家介绍的非常详细,详细内容请阅读本文,希望能对你有所帮助... 一、外部 css(推荐方式)定义:将 CSS 代码保存为独立的 .css 文件,通过 <link> 标签

Python使用Reflex构建现代Web应用的完全指南

《Python使用Reflex构建现代Web应用的完全指南》这篇文章为大家深入介绍了Reflex框架的设计理念,技术特性,项目结构,核心API,实际开发流程以及与其他框架的对比和部署建议,感兴趣的小伙... 目录什么是 ReFlex?为什么选择 Reflex?安装与环境配置构建你的第一个应用核心概念解析组件

C#通过进程调用外部应用的实现示例

《C#通过进程调用外部应用的实现示例》本文主要介绍了C#通过进程调用外部应用的实现示例,以WINFORM应用程序为例,在C#应用程序中调用PYTHON程序,具有一定的参考价值,感兴趣的可以了解一下... 目录窗口程序类进程信息类 系统设置类 以WINFORM应用程序为例,在C#应用程序中调用python程序

Java应用如何防止恶意文件上传

《Java应用如何防止恶意文件上传》恶意文件上传可能导致服务器被入侵,数据泄露甚至服务瘫痪,因此我们必须采取全面且有效的防范措施来保护Java应用的安全,下面我们就来看看具体的实现方法吧... 目录恶意文件上传的潜在风险常见的恶意文件上传手段防范恶意文件上传的关键策略严格验证文件类型检查文件内容控制文件存储

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2

在React聊天应用中实现图片上传功能

《在React聊天应用中实现图片上传功能》在现代聊天应用中,除了文字和表情,图片分享也是一个重要的功能,本文将详细介绍如何在基于React的聊天应用中实现图片上传和预览功能,感兴趣的小伙伴跟着小编一起... 目录技术栈实现步骤1. 消息组件改造2. 图片预览组件3. 聊天输入组件改造功能特点使用说明注意事项

Redis中RedisSearch使用及应用场景

《Redis中RedisSearch使用及应用场景》RedisSearch是一个强大的全文搜索和索引模块,可以为Redis添加高效的搜索功能,下面就来介绍一下RedisSearch使用及应用场景,感兴... 目录1. RedisSearch的基本概念2. RedisSearch的核心功能(1) 创建索引(2