Python实现windows自动关机功能

 更新时间:2025年01月15日 10:19:02   作者:Nobita Chen  
这篇文章主要为大家详细介绍了如何使用Python实现windows自动关机功能,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下

python <shut.py>

import ntplib
from datetime import datetime, timezone

import time
import os

import easygui

# net time
def get_network_time():
    time.sleep(3)
    """从网络时间服务器获取时间"""
    client = ntplib.NTPClient()
    response = client.request('pool.ntp.org')
    utc_time = datetime.utcfromtimestamp(response.tx_time)
    beijing_time = utc_time + timedelta(hours = 8)
    return beijing_time
    #return datetime.fromtimestamp(response.tx_time, timezone.utc)


local = datetime.now()
if local.hour >=0 and local.hour<7:
    print("当前时间处于0点至9点之间")
    os.system('shutdown /a')
    os.system('shutdown /s /t 60')
    easygui.msgbox("现在是休息时间,电脑即将关机,请你马上睡觉!!", "提示")
    easygui.msgbox("电脑即将关机!", "提示")
else:
    easygui.msgbox("当前时间不处于0点至7点之间=>local:[{}]".format(local.hour), "提示")
    os.system('shutdown /s /t 9000')

    # 下面的代码已经在关机后了~~~
    time.sleep(10)

    # 每隔3小时,执行关机
    while True:
        print("每隔3小时,执行关机")
        easygui.msgbox("电脑将在2.5小时后关闭,请注意休息~~", "提示")
        time.sleep(10800)
        os.system('shutdown /a')
        os.system('shutdown /s /t 60')

'''
try:
    
    # 获取网络时间
    current_time = get_network_time()

    # 提取小时数
    hour = current_time.hour
    

    # 判断是否处于0点至7点之间
    if 0 <= hour < 7:
        print("当前时间处于0点至7点之间")
        os.system('shutdown /a')
        os.system('shutdown /s /t 60')
        easygui.msgbox("现在是休息时间,电脑即将关机,请你马上睡觉!!", "提示")
        easygui.msgbox("电脑即将关机!", "提示")
    else:
        easygui.msgbox("当前时间不处于0点至7点之间=>remote[{}]".format(hour), "提示")
        os.system('shutdown /s /t 9000')
   

except:
    print('网络异常!!')

'''

vbe文件

Set ws = WScript.createObject("WScript.Shell")
ws.run "D:/data\Python/cmd.bat",0

<cmd.bat>

@echo off
title 执行Python脚本

c: & cd c:\Users\Administrator\AppData\Local\Programs\Python\Python37
python "D:\data\Python\shut.py" >>"D:\data\Python\%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%-%time:~3,2%-%time:~6,2%.txt"
::pause

计划任务,批处理,避免冲突

@echo off
::由于shutdown不能执行多次,所以先取消后,再次执行!

​​​​​​​shutdown /a
shutdown /s /t 30

方法补充

除了上文的方法,下面小编为大家整理了其他的Python实现Windows自动关机的方法,希望对大家有所帮助

完整代码

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'shut.ui'
#
# Created: Mon Mar 20 18:10:31 2017
#      by: PyQt5 UI code generator 5.2.1
#
# WARNING! All changes made in this file will be lost!
import sys
import os
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_shut(object):
    flag = True
    def setupUi(self, shut):
        shut.setObjectName("shut")
        shut.resize(411, 170)
        shut.setFixedSize(411,170)
        self.label = QtWidgets.QLabel(shut)
        self.label.setGeometry(QtCore.QRect(40, 50, 41, 51))
        self.label.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold))
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(shut)
        self.lineEdit.setGeometry(QtCore.QRect(70, 50, 71, 41))
        self.lineEdit.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold))
        self.lineEdit.setObjectName("lineEdit")
        self.label_2 = QtWidgets.QLabel(shut)
        self.label_2.setGeometry(QtCore.QRect(150, 60, 31, 31))
        self.label_2.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold))
        self.label_2.setObjectName("label_2")
        self.lineEdit_2 = QtWidgets.QLineEdit(shut)
        self.lineEdit_2.setGeometry(QtCore.QRect(180, 50, 71, 41))
        self.lineEdit_2.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.label_3 = QtWidgets.QLabel(shut)
        self.label_3.setGeometry(QtCore.QRect(260, 60, 31, 31))
        self.label_3.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold))
        self.label_3.setObjectName("label_3")
        self.pushButton = QtWidgets.QPushButton(shut,clicked=self.sd)
        self.pushButton.setGeometry(QtCore.QRect(290, 50, 101, 41))
        self.pushButton.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold))
        self.pushButton.setObjectName("pushButton")
        self.label_4 = QtWidgets.QLabel(shut)
        self.label_4.setGeometry(QtCore.QRect(0, 120, 411, 31))
        self.label_4.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold))
        self.label_4.setObjectName("label_4")
        
        self.retranslateUi(shut)
        QtCore.QMetaObject.connectSlotsByName(shut)

    def retranslateUi(self, shut):
        _translate = QtCore.QCoreApplication.translate
        shut.setWindowTitle(_translate("shut", "Auto Shutdown by dearvee"))
        self.label.setText(_translate("shut", "At:"))
        self.label_2.setText(_translate("shut", "H"))
        self.label_3.setText(_translate("shut", "M"))
        self.label_4.setText(_translate("shut", "Please input time of shutdown~"))
        self.pushButton.setText(_translate("shut", "Set"))

    def sd(self,shut):
        h = self.lineEdit.text()
        m = self.lineEdit_2.text()
        if self.flag:
            self.flag = False
            try:
                os.popen('at '+h+':'+m+' shutdown -s')
                self.label_4.setText('Success! the system will shutdown at today '+h+':'+m+'.')
                self.pushButton.setText('Remove all')
                self.lineEdit.clear()
                self.lineEdit_2.clear()
            except:
                self.label_4.setText('Something is wrong~')
        else:
            self.flag = True
            try:
                os.popen('at /delete /yes')
                self.label_4.setText('Success! already removed~')
                self.pushButton.setText('Set')
                self.lineEdit.clear()
                self.lineEdit_2.clear()
            except:
                self.label_4.setText('Something is wrong~')
        
if __name__ == '__main__':   
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_shut()
    ui.setupUi(Form) 
    Form.show()
    sys.exit(app.exec_())

到此这篇关于Python实现windows自动关机功能的文章就介绍到这了,更多相关Python windows自动关机内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Flask数据库迁移简单介绍

    Flask数据库迁移简单介绍

    这篇文章主要为大家详细介绍了Flask数据库迁移简单工作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Python开发中“切片创建副本但未赋值,以为修改原对象”的问题解决方法

    Python开发中“切片创建副本但未赋值,以为修改原对象”的问题解决方法

    Python开发中切片操作容易导致"修改原对象"的误解,本文详细分析了这一常见陷阱,通过典型场景复现问题,解释切片表达式产生新对象的底层机制,并给出正确修改原对象或保存副本的操作方法
    2026-06-06
  • python命令行执行方法及技巧分享

    python命令行执行方法及技巧分享

    这篇文章主要介绍了Python编程中使用命令行工具的基本操作和进阶技巧,通过编写Python脚本,理解Python解释器,以及在命令行中执行脚本,可以实现数据处理和系统任务的自动化,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-04-04
  • python开发之函数定义实例分析

    python开发之函数定义实例分析

    这篇文章主要介绍了python开发之函数定义方法,以实例形式较为详细的分析了Python中函数的定义与使用技巧,需要的朋友可以参考下
    2015-11-11
  • Windows下Sqlmap环境安装教程详解

    Windows下Sqlmap环境安装教程详解

    这篇文章主要介绍了Windows下Sqlmap环境安装,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • python使用百度文字识别功能方法详解

    python使用百度文字识别功能方法详解

    在本篇文章里小编给大家整理的是关于python怎么使用百度文字识别功能的相关知识点,有兴趣的朋友们参考下。
    2019-07-07
  • Python SSL证书验证问题解决方案

    Python SSL证书验证问题解决方案

    这篇文章主要介绍了Python SSL证书验证问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • Python实用工具之实现PDF转DOCX文档

    Python实用工具之实现PDF转DOCX文档

    pdf2docx作为第三方包,提供了非常优秀的功能,仅仅几行代码就可以完成PDF转换为DOCX的工作,所以本文就来利用pdf2docx实现PDF转DOCX文档功能吧
    2023-12-12
  • python提取字典key列表的方法

    python提取字典key列表的方法

    这篇文章主要介绍了python提取字典key列表的方法,涉及Python中keys方法的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • TensorFlow2基本操作之合并分割与统计

    TensorFlow2基本操作之合并分割与统计

    这篇文章主要介绍了TensorFlow2基本操作之合并分割与统计,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09

最新评论