Python實現(xiàn)Windows定時關(guān)機

筆者由Qt制作完成需要的ui,包括label,label_2,label_3,lable_4,lineEdit,lineEdit_2,pushButton組件.大致布局如下

兩個lineEdit等待用戶輸入期望關(guān)機的時間。下面的Label用來顯示操作后的返回信息。pushButton用于提交命令。ui制作完成。

ui轉(zhuǎn)為py文件:

  這里筆者裝的是PyQt5,并添加了環(huán)境變量。所以轉(zhuǎn)化的cmd命令(cd到ui所在目錄):

pyuic5 shut.ui -o shut.py

執(zhí)行成功之后在ui所在目錄生成shut.py文件。

顯示窗口:

  直接生成的py文件運行是看不到窗口的,我們要加上一些必要的內(nèi)容才能顯示我們的窗口:

?代碼最上面加上

importsys

最后加上

if__name__=='__main__':?

? ? app = QtWidgets.QApplication(sys.argv)

? ? Form = QtWidgets.QWidget()

? ? ui = Ui_x()//其中Ui_x為生成的class名

? ? ui.setupUi(Form)

? ? Form.show()

? ? sys.exit(app.exec_())

之后再運行shut.py就能看到窗口了。

#功能實現(xiàn):

  思考一下程序的期望功能,使Windows自動關(guān)機。cmd命令是個不錯的選擇。于是筆者找了下,python執(zhí)行cmd命令的方法:

os.popen('at 22:30 shutdown -s')

調(diào)用cmd,執(zhí)行命令。而其中的22和30是等待用戶輸入的數(shù)據(jù)。因此,應(yīng)該用兩個lineEdit中獲取到的合法數(shù)字替換對應(yīng)的h和m。用到獲取lineEdit內(nèi)容的方法:

h = self.lineEdit.text()

m = self.lineEdit_2.text()

然后以h,m替換執(zhí)行命令中的時,分.

接著就是pushButton的部分了。為pushButton添加監(jiān)聽事件click。

self.pushButton = QtWidgets.QPushButton(shut,clicked=self.sd)

其中,self.sd為觸發(fā)該事件后,需要執(zhí)行的操作。


完整代碼:

  一些關(guān)鍵的部分,敘述完畢,至于返回信息部分,筆者在這里不再詳述。下面貼出來Windows自動關(guān)機完整的代碼:

# -*- 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 sysimport osfromPyQt5import QtCore, QtGui, QtWidgetsclass 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_())

運行后,即出現(xiàn)如圖操作窗口



運行效果:

運行shut.py,輸入12和53點擊set,這時我們查看任務(wù)計劃:

發(fā)現(xiàn)任務(wù)已經(jīng)在計劃中。點擊Remove,刷新任務(wù)計劃。

成功移除任務(wù),功能實現(xiàn)

當(dāng)然這只能在用戶安裝Python,并安裝相關(guān)組件前提下才可運行。想要在任何windows使用,則需要下面的操作。

#打包:

  筆者打包用的是Python的Pyinstaller組件。cd 到shut.py所在目錄后,執(zhí)行cmd命令:

pyinstaller -w shut.py

這時,在shut.py所在目錄生成dist文件夾。生成的exe路徑。dist>>shut(Python源碼文件名)>>shut.exe.前面順利的話,雙擊shut.exe便會顯示前面源碼運行同樣的窗口和操作。這樣,你就可以把shut目錄整個發(fā)給你的朋友。他們就可以通過雙擊shut.exe使用你的程序了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容