SoFunction
Updated on 2025-03-05

Python implements windows automatic shutdown function

python <>

import ntplib
from datetime import datetime, timezone

import time
import os

import easygui

# net time
def get_network_time():
    (3)
    """Get time from network time server"""
    client = ()
    response = ('')
    utc_time = (response.tx_time)
    beijing_time = utc_time + timedelta(hours = 8)
    return beijing_time
    #return (response.tx_time, )


local = ()
if  &gt;=0 and &lt;7:
    print("The current time is between 0 and 9 o'clock")
    ('shutdown /a')
    ('shutdown /s /t 60')
    ("It's time to rest now, the computer is about to turn off, please go to bed right away!!", "hint")
    ("The computer is about to shut down!", "hint")
else:
    ("The current time is not in0Click to7Between the points=&gt;local:[{}]".format(), "hint")
    ('shutdown /s /t 9000')

    # The following code has been shut down~~~    (10)

    # Every 3 hours, shut down    while True:
        print("Every 3 hours, shut down")
        ("The computer will be turned off after 2.5 hours, please pay attention to rest~~", "hint")
        (10800)
        ('shutdown /a')
        ('shutdown /s /t 60')

'''
try:
    
    # Get network time    current_time = get_network_time()

    # Extract hours    hour = current_time.hour
    

    # Determine whether it is between 0 and 7 o'clock    if 0 &lt;= hour &lt; 7:
        print("The current time is between 0 and 7 o'clock")
        ('shutdown /a')
        ('shutdown /s /t 60')
        ("It's time to rest now, the computer is about to turn off, please go to bed right away!!", "hint")
        ("The computer is about to shut down!", "hint")
    else:
        ("The current time is not in0Click to7Between the points=&gt;remote[{}]".format(hour), "hint")
        ('shutdown /s /t 9000')
   

except:
    print('Network exception!  !  ')

'''

vbe file

Set ws = ("")
 "D:/data\Python/",0

<>

@echo off
title implementPythonscript

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

Plan tasks, batch processing, avoid conflicts

@echo off
::becauseshutdownCannot execute multiple times,So cancel first,Execute again!

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

Method supplement

Complete code

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

# Form implementation generated from reading ui file ''
#
# 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")
        (411, 170)
        (411,170)
         = (shut)
        ((40, 50, 41, 51))
        (("Roman times",10,))
        ("label")
         = (shut)
        ((70, 50, 71, 41))
        (("Roman times",10,))
        ("lineEdit")
        self.label_2 = (shut)
        self.label_2.setGeometry((150, 60, 31, 31))
        self.label_2.setFont(("Roman times",10,))
        self.label_2.setObjectName("label_2")
        self.lineEdit_2 = (shut)
        self.lineEdit_2.setGeometry((180, 50, 71, 41))
        self.lineEdit_2.setFont(("Roman times",10,))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.label_3 = (shut)
        self.label_3.setGeometry((260, 60, 31, 31))
        self.label_3.setFont(("Roman times",10,))
        self.label_3.setObjectName("label_3")
         = (shut,clicked=)
        ((290, 50, 101, 41))
        (("Roman times",10,))
        ("pushButton")
        self.label_4 = (shut)
        self.label_4.setGeometry((0, 120, 411, 31))
        self.label_4.setFont(("Roman times",10,))
        self.label_4.setObjectName("label_4")
        
        (shut)
        (shut)

    def retranslateUi(self, shut):
        _translate = 
        (_translate("shut", "Auto Shutdown by dearvee"))
        (_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~"))
        (_translate("shut", "Set"))

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

This is the end of this article about Python's automatic shutdown function. For more related content on Python's automatic shutdown, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!