通過Python實(shí)現(xiàn)電腦定時(shí)關(guān)機(jī)的兩種方法
導(dǎo)語
無論家用電腦還是公司的電腦,定時(shí)開關(guān)機(jī)都是一個(gè)非常實(shí)用的功能,只是一般都不太受關(guān)注。定時(shí)關(guān)機(jī)不僅能延長電腦的使用壽命,還能節(jié)約超多的電費(fèi)呢~哈哈哈哈?
害~這不,周天休假一天,但是公司的電腦大部分的小伙伴兒就開了一天,有時(shí)候放長假的時(shí)候電腦一開就是幾天。這不?隔壁的小姐姐已經(jīng)被領(lǐng)導(dǎo)發(fā)現(xiàn)了,最后的最后這個(gè)事情就落到我手里了,開發(fā)一個(gè)能定時(shí)關(guān)機(jī)的源碼項(xiàng)目供大家使用,并互相提醒每個(gè)人一定要下班關(guān)機(jī)!
定時(shí)開關(guān)機(jī)是個(gè)非常簡單又實(shí)用的功能,跟著本文用不了10分鐘你就能學(xué)會(huì)如何進(jìn)行操作了哈~

本文介紹了:普通人關(guān)機(jī)的習(xí)慣方式、以及程序員關(guān)機(jī)的方式哈!主頁(pc端)左側(cè)有源碼滴~
并且程序員關(guān)機(jī)附了2份代碼喜歡tkinter的就用tk版本的,喜歡pyqt5的就用這版本的。兩個(gè)版本大家自己愛用那個(gè)用那個(gè)哈~
一、普通人關(guān)機(jī)
0)優(yōu)雅的關(guān)機(jī)

1)快捷鍵關(guān)機(jī)
使用快捷鍵關(guān)機(jī)相信是大家比較常用的方法了吧?也是最簡單方便的方法。
按下快捷鍵【Alt】+【F4】彈出功能對(duì)話框,實(shí)現(xiàn)快速關(guān)機(jī),又快又準(zhǔn)效率極高。

2)任務(wù)管理器關(guān)機(jī)
這個(gè)方法和上一個(gè)快捷鍵關(guān)機(jī)的方法有著異曲同工之妙,按下快捷鍵【Alt】+【Ctrl】+
【Delete】,打開任務(wù)管理器,右下方有個(gè)圓形關(guān)機(jī)按鈕,點(diǎn)擊即可關(guān)機(jī)。

3)設(shè)置關(guān)機(jī)時(shí)間
這個(gè)方法可以實(shí)現(xiàn)定時(shí)關(guān)機(jī),算好想要關(guān)機(jī)的時(shí)間,設(shè)置完成后電腦到點(diǎn)就會(huì)自動(dòng)關(guān)機(jī)。再也不用因?yàn)殡娔X關(guān)機(jī)更新而等個(gè)半天了。按下快捷鍵【win】+【R】,在運(yùn)行對(duì)話框內(nèi)輸入《at 20:00 shutdown -s》后點(diǎn)擊確定,系統(tǒng)就會(huì)在晚上8點(diǎn)準(zhǔn)時(shí)關(guān)機(jī)。這個(gè)方法可以在大家確定能夠關(guān)機(jī)時(shí)間的情況下使用還是非常方便的。

4)鼠標(biāo)滑動(dòng)關(guān)機(jī)
在電腦桌面位置創(chuàng)建一個(gè)快捷方式,在出現(xiàn)的對(duì)話框內(nèi)輸入“slidetoshutdown”后點(diǎn)下一步。

點(diǎn)擊完成即可。
此時(shí)電腦桌面上就多了一個(gè)快捷方式,想要關(guān)機(jī)時(shí)雙擊該快捷方式,就會(huì)出現(xiàn)以下提示,滑動(dòng)就可以關(guān)閉電腦了。

二、程序員關(guān)機(jī)
嘿嘿~雖然確實(shí)正常人開關(guān)機(jī)上面的更方便快速哈!但是源碼打包成exe點(diǎn)開更方便啦~尤其是在電腦需要幾個(gè)小時(shí)之后關(guān)機(jī)的,需要先讓電腦自動(dòng)完成任務(wù)再關(guān)機(jī)滴~吶這個(gè)還蠻方便的~也許,應(yīng)該更方便.jpg 強(qiáng)行解釋的我2333~這就是娛樂娛樂的代碼哈哈哈哈~
1)Pyqt5界面化小程序
1.1? 附源碼項(xiàng)目
# -*—coding:utf-8 -*-
import sys
import os # Python執(zhí)行系統(tǒng)命令方法所用到的包
from PyQt5 import QtCore,QtGui,QtWidgets
class Ui_shut(object): #類 繼承object類
flag = True
def setupUi(self,shut): #方法
#設(shè)置窗體的大小
shut.setObjectName("shut")
shut.resize(420,180)
shut.setFixedSize(420,180)
self.label = QtWidgets.QLabel(shut)
self.label.setGeometry(QtCore.QRect(40,50,41,51)) #標(biāo)簽的位置
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) #為pushButton添加監(jiān)聽事件click。
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, 500, 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) #connectSlotsByName是一個(gè)QMetaObject類里的靜態(tài)函數(shù),其作用是用來將QObject * o里的子QObject的某些信號(hào)按照其objectName連接到o的槽上。
def retranslateUi(self,shut):
_translate = QtCore.QCoreApplication.translate
shut.setWindowTitle(_translate("shut", "Windows定時(shí)關(guān)機(jī)器"))
self.label.setText(_translate("shut", "在:"))
self.label_2.setText(_translate("shut", "時(shí)"))
self.label_3.setText(_translate("shut", "分"))
self.label_4.setText(_translate("shut", " 請(qǐng)輸入關(guān)機(jī)時(shí)間"))
self.pushButton.setText(_translate("shut", "設(shè)置"))
def sd(self,shut): #self.sd為觸發(fā)該事件后,需要執(zhí)行的操作。
h = self.lineEdit.text()
m = self.lineEdit_2.text()
if self.flag:
self.flag = False
try: #捕獲所有異常
os.popen('at'+ h + ':' + m + ' shutdown -s') #python執(zhí)行cmd命令的方法
self.label_4.setText(' 設(shè)置成功! 系統(tǒng)將關(guān)機(jī)在今天 '+h+':'+m)
self.pushButton.setText('移除')
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('成功,全部移除')
self.pushButton.setText('Set')
self.lineEdit.clear()
self.lineEdit_2.clear()
except:
self.label_4.setText('Something is wrong')
1.2? 效果展示

?2)Tkinter界面化小程序
2.1? 附源碼項(xiàng)目
from tkinter import ttk
import os
import tkinter.messagebox as message_box
windows = tkinter.Tk()
windows.title("Python定時(shí)關(guān)機(jī)")
# window 居中
windows.update() # update window ,must do
curWidth = 280 # get current width
curHeight = windows.winfo_reqheight() # get current height
scnWidth, scnHeight = windows.maxsize() # get screen width and height
# now generate configuration information
config = '%dx%d+%d+%d' % (curWidth, curHeight,
(scnWidth - curWidth) / 2, (scnHeight - curHeight) / 2)
windows.geometry(config)
# root 容器
root = ttk.LabelFrame(windows, text="關(guān)機(jī)命令")
root.grid(column=0, row=0, padx=15, pady=15)
# 提醒文本
tkinter.Label(root, text="輸入時(shí)間").grid(column=0, row=0, sticky=tkinter.W)
tkinter.Label(root, text="選擇").grid(column=1, row=0)
# 存儲(chǔ)輸入的值
time = tkinter.StringVar()
unit = tkinter.StringVar()
# 輸入框
time_edit = tkinter.Entry(root, width=10, textvariable=time)
time_edit.grid(column=0, row=1, padx=4, sticky=tkinter.W)
time_edit.focus()
# 下拉單位選擇
unit_arr = ('時(shí)', '分', '秒')
unit_chosen = ttk.Combobox(root, width=6, textvariable=unit, state='readonly')
unit_chosen['values'] = unit_arr
unit_chosen.grid(column=1, row=1)
unit_chosen.current(0)
def change_edit(to_time):
time_edit.delete(0, 10)
time_edit.insert(0, to_time)
unit_chosen.current(1)
# start
def start():
if time.get() and unit.get():
message_box.showwarning("選擇完畢", "你的電腦將在多少 %s %s" % (time.get(), unit.get()))
# shutdown 的秒數(shù)
count_down_second = int(time.get())
if unit.get() == 'hour':
count_down_second *= 3600
elif unit.get() == 'minute':
count_down_second *= 60
# execute
os.system("shutdown -s -t %s" % count_down_second)
windows.quit()
# cancel
def cancel():
os.system("shutdown -a")
windows.quit()
# start 按鈕
start_action = tkinter.Button(root, text="START", command=start)
start_action.grid(column=2, row=1)
# 文本
tip_label = tkinter.Label(root, text="倒計(jì)時(shí)關(guān)機(jī)")
tip_label.grid(row=2, column=0, pady=2)
# 快捷選擇時(shí)間
fram = tkinter.Frame(root)
fram.grid(row=3, column=0, columnspan=3)
# 常用的時(shí)間
for i in range(2, 7):
button = tkinter.Button(fram, text=str(i * 15) + "min", command=lambda x=i: change_edit(str(x * 15)))
button.grid(row=0, column=i - 2, padx=2, pady=2, sticky=tkinter.W)
# cancel 按鈕
cancel_action = tkinter.Button(root, text="CANCEL", command=lambda: cancel())
cancel_action.grid(row=4, column=1, pady=10, sticky=tkinter.W)
2.2? 效果展示

以上就是通過Python實(shí)現(xiàn)電腦定時(shí)關(guān)機(jī)的兩種方法的詳細(xì)內(nèi)容,更多關(guān)于Python電腦定時(shí)關(guān)機(jī)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pytorch 改變tensor尺寸的實(shí)現(xiàn)
今天小編就為大家分享一篇pytorch 改變tensor尺寸的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python3非對(duì)稱加密算法RSA實(shí)例詳解
這篇文章主要介紹了Python3非對(duì)稱加密算法RSA,結(jié)合實(shí)例形式分析了Python3 RSA加密相關(guān)模塊安裝及使用操作技巧,需要的朋友可以參考下2018-12-12
python經(jīng)典趣味24點(diǎn)游戲程序設(shè)計(jì)
這篇文章主要介紹了python經(jīng)典趣味24點(diǎn)游戲程序設(shè)計(jì),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
使用python解析MDX詞典數(shù)據(jù)并保存為Excel文件
MDX(Mobile Dictionary eXchange)是一種常見的詞典文件格式,通常用于在移動(dòng)設(shè)備和電腦之間共享辭典數(shù)據(jù),本文深入探討了從MDX詞典數(shù)據(jù)提取、處理到最終保存為Excel文件的全過程,需要的朋友可以參考下2023-12-12
用pickle存儲(chǔ)Python的原生對(duì)象方法
下面小編就為大家?guī)硪黄胮ickle存儲(chǔ)Python的原生對(duì)象方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Scrapy之爬取結(jié)果導(dǎo)出為Excel的實(shí)現(xiàn)過程
這篇文章主要介紹了Scrapy之爬取結(jié)果導(dǎo)出為Excel的實(shí)現(xiàn)過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Python設(shè)計(jì)模式行為型責(zé)任鏈模式
這篇文章主要介紹了Python設(shè)計(jì)模式行為型責(zé)任鏈模式,責(zé)任鏈模式將能處理請(qǐng)求的對(duì)象連成一條鏈,并沿著這條鏈傳遞該請(qǐng)求,直到有一個(gè)對(duì)象處理請(qǐng)求為止,避免請(qǐng)求的發(fā)送者和接收者之間的耦合關(guān)系,下圍繞改內(nèi)容介紹具有一點(diǎn)的參考價(jià)值,需要的朋友可以參考下2022-02-02
Python實(shí)現(xiàn)統(tǒng)計(jì)單詞出現(xiàn)的個(gè)數(shù)
這篇文章主要介紹了Python實(shí)現(xiàn)統(tǒng)計(jì)單詞出現(xiàn)的個(gè)數(shù),本文給出了實(shí)現(xiàn)代碼以及使用方法,需要的朋友可以參考下2015-05-05

