Python實現(xiàn)windows自動關機功能
更新時間:2025年01月15日 10:19:02 作者:Nobita?Chen
這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)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("現(xiàn)在是休息時間,電腦即將關機,請你馬上睡覺!!", "提示")
easygui.msgbox("電腦即將關機!", "提示")
else:
easygui.msgbox("當前時間不處于0點至7點之間=>local:[{}]".format(local.hour), "提示")
os.system('shutdown /s /t 9000')
# 下面的代碼已經在關機后了~~~
time.sleep(10)
# 每隔3小時,執(zhí)行關機
while True:
print("每隔3小時,執(zhí)行關機")
easygui.msgbox("電腦將在2.5小時后關閉,請注意休息~~", "提示")
time.sleep(10800)
os.system('shutdown /a')
os.system('shutdown /s /t 60')
'''
try:
# 獲取網絡時間
current_time = get_network_time()
# 提取小時數(shù)
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("現(xiàn)在是休息時間,電腦即將關機,請你馬上睡覺??!", "提示")
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 執(zhí)行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不能執(zhí)行多次,所以先取消后,再次執(zhí)行! ???????shutdown /a shutdown /s /t 30
方法補充
除了上文的方法,下面小編為大家整理了其他的Python實現(xiàn)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實現(xiàn)windows自動關機功能的文章就介紹到這了,更多相關Python windows自動關機內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python數(shù)據(jù)可視化Seaborn畫熱力圖
這篇文章主要介紹了數(shù)據(jù)可視化Seaborn畫熱力圖,熱力圖的想法其實很簡單,用顏色替換數(shù)字,下面我們來看看文章對操作過程的具體介紹吧,需要的小伙伴可以參考一下具體內容,希望對你有所幫助2022-01-01
python 應用之Pycharm 新建模板默認添加編碼格式-作者-時間等信息【推薦】
這篇文章主要介紹了Pycharm 新建模板默認添加編碼格式-作者-時間等信息 ,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06
python實現(xiàn)對圖片進行旋轉,放縮,裁剪的功能
今天小編就為大家分享一篇python實現(xiàn)對圖片進行旋轉,放縮,裁剪的功能,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08

