Python之电脑好帮手—自动定时关机

咳咳咳,最近脑子里蹦出个新的想法:
能不能试试用Python写一个关机脚本定时关机呢?

本人菜鸟一枚,勿喷。
根据我学了的库来说OS库解决了我的难题,可以直接使用:

os.system('shutdown -s ')

直接关机…..妙啊~~~~~

一、你需要准备

  • Python3编译器
  • Pycharm2020.3

    (有条件者推荐使用)

  • 第三方Python库——

    Pywin32

二、下载第三方库——Pywin32

  • 同时摁下win+r键,打开运行

Python之电脑好帮手—自动定时关机

  • 输入cmd,回车【Enter】
  • 输入pip install pywin32

Python之电脑好帮手—自动定时关机

  • 等待几秒即可

三、设置函数__shutdown__.py控制关机

#******************************************************************
#本程序由.cn作者:yuyu226编译
#Python之电脑好帮手—自动定时关机     作者:yuyu226
#盗版必究
#******************************************************************
import time
import win32api
import os
import datetime

def __shutdown__():
    with open('timea.txt', 'r')as d:       #打开timea.txt文件读取
       stime = d.read()  # 全部读取,设置时间
       d.close()


    with open('timeb.txt','r')as x:        #打开timeb.txt文件读取
        stime2= x.read() # 全部读取,设置时间
        x.close()
        
    win32api.MessageBox( win32con.NULL,u'成功启动,作者:yuyu226(.cn)',u'温馨提醒')       #win32温馨提醒窗口

    daojishi=59 #给使用者留下倒计时,方便使用者
    while True:
        now_time=(time.strftime('%H:%M'))  #获取当前时间
        time.sleep(1)      #各一分钟获取一次时间,防止cup跑炸

        if now_time==stime:
            os.system('shutdown -s -t 59')    #os 关机命令
            win32api.MessageBox( win32con.NULL,u'还有%as关机,赶快保存文件!作者:yuyu226(.cn)'%daojishi,u'温馨提醒')
            
            break

        if now_time==stime2:
            os.system('shutdown -s -t 59')
            win32api.MessageBox(win32con.NULL,u'还有%as关机,赶快保存文件!作者:yuyu226(.cn)'%daojishi,u'温馨提醒')
            break

    while True:
        time.sleep(1)
        daojishi=daojishi-1
        win32api.MessageBox(win32con.NULL,u'还有%as关机,赶快保存文件!作者:yuyu226(.cn)'%daojishi,u'温馨提醒')
        if daojishi==1:
           win32api.MessageBox( u'正在关机,作者:yuyu226(.cn)', u'温馨提醒', win32con.MB_OK)

#**********************************************************************
#喜爱的就给作者双击评分哦(.cn)
#**********************************************************************

三、主控程序shutdown_model.py控制

#******************************************************************
#本程序由.cn作者:yuyu226编译
#Python之电脑好帮手—自动定时关机     作者:yuyu226
#盗版必究
#******************************************************************
from __shutdown__ import __shutdown__
if __name__=='__main__':
    __shutdown__()
#**********************************************************************
#喜爱的就给作者双击评分哦(.cn)
#**********************************************************************

Tip:想要更改关机时间就去更改timea或timeb.txt的时间

四、打包exe运行

  • 同时摁下win+r键,打开运行

  • 输入cmd,回车【Enter】
  • 输入pyinstaller -F shutdown_model.py -w
D:Pycharm 2020.3>pyinstaller -F shutdown.py -w
139 INFO: PyInstaller: 4.1
140 INFO: Python: 3.8.3
140 INFO: Platform: Windows-10-10.0.17134-SP0
142 INFO: wrote D:Pycharm 2020.3shutdown.spec
176 INFO: UPX is not available.
script 'D:Pycharm 2020.3shutdown.py' not found
D:Pycharm 2020.3>pyinstaller -F shutdown_model.py -w
147 INFO: PyInstaller: 4.1
147 INFO: Python: 3.8.3
148 INFO: Platform: Windows-10-10.0.17134-SP0
150 INFO: wrote D:Pycharm 2020.3shutdown_model.spec
158 INFO: UPX is not available.
162 INFO: Extending PYTHONPATH with paths
['D:\Pycharm 2020.3', 'D:\Pycharm 2020.3']
pygame 2.0.0 (SDL 2.0.12, python 3.8.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
1526 INFO: checking Analysis
1548 INFO: Building because hookspath changed
1548 INFO: Initializing module dependency graph...
1573 INFO: Caching module graph hooks...
1627 WARNING: Several hooks defined for module 'pygame'. Please take care they do not conflict.
1675 INFO: Analyzing base_library.zip ...
5334 INFO: Processing pre-find module path hook distutils from 'd:\python3.8\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-distutils.py'.
5365 INFO: distutils: retargeting to non-venv dir 'd:\python3.8\lib'
10949 INFO: Caching module dependency graph...
11237 INFO: running Analysis Analysis-00.toc
11310 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by d:python3.8python.exe
12534 INFO: Analyzing D:Pycharm 2020.3shutdown_model.py
12546 INFO: Processing module hooks...
12548 INFO: Loading module hook 'hook-difflib.py' from 'd:\python3.8\lib\site-packages\PyInstaller\hooks'...
12602 INFO: Excluding import of doctest from module difflib
12603 INFO: Loading module hook 'hook-distutils.py' from 'd:\python3.8\lib\site-packages\PyInstaller\hooks'...
12651 INFO: Loading module hook 'hook-distutils.util.py' from 'd:\python3.8\lib\site-packages\PyInstaller\hooks'...
12655 INFO: Excluding import of lib2to3.refactor from module distutils.util
12655 INFO: Loading module hook 'hook-encodings.py' from 'd:\python3.8\lib\site-packages\PyInstaller\hooks'...
12906 INFO: Loading module hook 'hook-heapq.py' from 'd:\python3.8\lib\site-packages\PyInstaller\hooks'...
12910 INFO: Excluding import of doctest from module heapq
12911 INFO: Loading module hook 'hook-lib2to3.py' from 'd:\python3.8\lib\site-packages\PyInstaller\hooks'...
13124 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'd:\python3.8\lib\site-packages\PyInstaller\hooks'...
13147 INFO: Excluding import of test from module multiprocessing.util
13147 INFO: Excluding import of test.support from module multiprocessing.util
13148 INFO: Loading module hook 'hook-pickle.py' from 'd:\python3.8\lib\site-packages\PyInstaller\hooks'...
13154 INFO: Excluding import of argparse from module pickle
13156 INFO: Loading module hook 'hook-sysconfig.py' from 'd:\python3.8\lib\site-packages\PyInstaller\hooks'...
13160 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'd:\python3.8\lib\site-packages\PyInstaller\hooks'...
13164 INFO: Loading module hook 'hook-xml.py' from 'd:\python3.8\lib\site-packages\PyInstaller\hooks'...
13293 INFO: Loading module hook 'hook-_tkinter.py' from 'd:\python3.8\lib\site-packages\PyInstaller\hooks'...
13851 INFO: checking Tree
13898 INFO: Building because root changed
13899 INFO: Building Tree Tree-00.toc
14323 INFO: checking Tree
14346 INFO: Building because root changed
14346 INFO: Building Tree Tree-01.toc
14588 INFO: checking Tree
14590 INFO: Building because root changed
14590 INFO: Building Tree Tree-02.toc
14611 INFO: Looking for ctypes DLLs
14645 INFO: Analyzing run-time hooks ...
14650 INFO: Including run-time hook 'd:\python3.8\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_multiprocessing.py'
14669 INFO: Including run-time hook 'd:\python3.8\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_win32api.py'
14698 INFO: Looking for dynamic libraries
15608 INFO: Looking for eggs
15608 INFO: Using Python library d:python3.8python38.dll
15610 INFO: Found binding redirects:
[]
15615 INFO: Warnings written to D:Pycharm 2020.3uildshutdown_modelwarn-shutdown_model.txt
15665 INFO: Graph cross-reference written to D:Pycharm 2020.3uildshutdown_modelxref-shutdown_model.html
15725 INFO: checking PYZ
15759 INFO: Building because toc changed
15759 INFO: Building PYZ (ZlibArchive) D:Pycharm 2020.3uildshutdown_modelPYZ-00.pyz
16467 INFO: Building PYZ (ZlibArchive) D:Pycharm 2020.3uildshutdown_modelPYZ-00.pyz completed successfully.
16479 INFO: checking PKG
16490 INFO: Building because toc changed
16490 INFO: Building PKG (CArchive) PKG-00.pkg
20694 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
20698 INFO: Bootloader d:python3.8libsite-packagesPyInstallerootloaderWindows-64bit
unw.exe
20699 INFO: checking EXE
20717 INFO: Building because icon changed
20718 INFO: Building EXE from EXE-00.toc
20749 INFO: Appending archive to EXE D:Pycharm 2020.3distshutdown_model.exe
20773 INFO: Building EXE from EXE-00.toc completed successfully.

  • 等待几秒即可

___________________________成品下载地址_______________________________________________

土豪VIP下载:

小白直达:

链接:https://pan.baidu.com/s/1H8weTDh52awmE1fEgaTPag

提取码:znkq

————————————————————————————————————————————————
喜爱就点个评个分哦,么么哒~~~

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 共37条

请登录后发表评论