使用Python编写电脑定时关机小程序
这是一个Python应用。家里卧室装了新电视,HDMI连接笔记本追剧还是很愉快的。可是经常睡着,自然忘了关机。搜了一大圈,都是用命令行或者bat解决。商店里的应用也不好用,有些还收费。于是萌生了自己写一个定时关机应用的想法。利用Notebook实现“默认模式”和“自定义模式”选项卡,如图所示。最后一张图是素材。
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | import datetime import tkinter as tk from tkinter import ttk from threading import Thread import time import os class ShutdownApp: def __init__( self , root): self .time_left = None self .root = root self .root.title( "定时关机应用" ) self .root.resizable( 0 , 0 ) screenwidth = self .root.winfo_screenwidth() screenheight = self .root.winfo_screenheight() width = 600 height = 200 size_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/Desktop/icon.png' )) self .root[ "background" ] = "#8DB6CD" self .notebook = tk.ttk.Notebook( self .root) self .framework1 = tk.Frame() self .framework2 = tk.Frame() self .notebook.add( self .framework1, text = '默认模式' ) self .notebook.add( self .framework2, text = '自定义模式' ) self .notebook.pack(padx = 10 , pady = 5 , fill = tk.BOTH, expand = True ) # 选项卡1的界面 tk.Label( self .framework1, text = "选择关机时长:" ).pack() cbox = ttk.Combobox( self .framework1) cbox[ 'value' ] = ( '0.5小时' , '1小时' , '1.5小时' , '2小时' ) cbox.current( 1 ) self .selected_value = cbox.get() 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_left2 = self .time_entry2.get() 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 = None self .running = False # 选项卡1的功能实现 def selected_time( self , selected_value): match selected_value: case '0.5小时' : self .time_left = 1800 case '1小时' : self .time_left = 3600 case '1.5小时' : self .time_left = 5400 case '2小时' : self .time_left = 7200 def start_timer( self ): try : self .selected_time( self .selected_value) except ValueError: self .timer_label.config(text = "请选择关机倒计时时长!" ) return self .notebook.tab( 1 , state = 'disabled' ) self .running = True self .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 ): while self .time_left > 0 and self .running: timer = str (datetime.timedelta(seconds = int ( self .time_left))) self .timer_label.config(text = f "关机倒计时: {timer} " , font = ( "黑体" , 45 ), fg = "white" , bg = "#8DB6CD" ) time.sleep( 1 ) self .time_left - = 1 self .timer_label.config(text = "") if self .running: os.system( "shutdown /s /t 1" ) # 在Windows上执行关机命令 def cancel_timer( self ): self .running = False self .start_button.config(state = 'normal' ) self .cancel_button.config(state = 'disabled' ) self .timer_label.config(text = "已取消关机" ) self .notebook.tab( 1 , state = 'normal' ) # 选项卡2的功能实现 def start_timer2( self ): try : self .time_left2 = int ( self .time_entry2.get()) except ValueError: self .timer_label2.config(text = "请输入有效的数字!" ) return self .notebook.tab( 0 , state = 'disabled' ) self .running = True self .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 ): while self .time_left2 > 0 and self .running: self .timer_label2.config(text = f "关机倒计时: {self.time_left2} 秒" , font = ( "黑体" , 45 ),fg = "white" , bg = "#8DB6CD" ) time.sleep( 1 ) self .time_left2 - = 1 self .timer_label2.config(text = "") if self .running: os.system( "shutdown /s /t 1" ) # 在Windows上执行关机命令 def cancel_timer2( self ): self .running = False self .start_button2.config(state = 'normal' ) self .cancel_button2.config(state = 'disabled' ) self .timer_label2.config(text = "已取消关机" ) self .notebook.tab( 0 , state = 'normal' ) if __name__ = = "__main__" : ui = tk.Tk() app = ShutdownApp(ui) ui.mainloop() |
到此这篇关于使用Python编写电脑定时关机小程序的文章就介绍到这了,更多相关Python定时关机内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
Python常见读写文件操作实例总结【文本、json、csv、pdf等】
这篇文章主要介绍了Python常见读写文件操作,结合实例形式总结分析了Python常见的各种文件读写操作,包括文本、json、csv、pdf等文件的读写与相关注意事项,需要的朋友可以参考下2019-04-04
最新评论