Python實現Windows定時關機


  

 

  是最初的幾個爬蟲,讓我認識了Python這個新朋友,雖然才剛認識了幾天,但感覺有種莫名的默契感。每當在別的地方找不到思路,總能在Python找到解決的辦法。自動關機,在平時下載大文件,以及跑程序的時候能用到的,剛才寫了個windows自動關機的小程序,程序過於簡單,就當是玩玩吧,當然還有很多可改進的地方。下面正文:(原創鏈接:http://www.cnblogs.com/dearvee/p/6593822.html)


 #ui制作:

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

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

#ui轉為py文件:

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

pyuic5 shut.ui -o shut.py

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

#顯示窗口:

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

 代碼最上面加上

import sys

最后加上

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就能看到窗口了。

#功能實現:

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

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

調用cmd,執行命令。而其中的22和30是等待用戶輸入的數據。因此,應該用兩個lineEdit中獲取到的合法數字替換對應的h和m。用到獲取lineEdit內容的方法:

h = self.lineEdit.text() m = self.lineEdit_2.text()

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

接着就是pushButton的部分了。為pushButton添加監聽事件click。

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

其中,self.sd為觸發該事件后,需要執行的操作。

 

#完整代碼:

  一些關鍵的部分,敘述完畢,至於返回信息部分,筆者在這里不再詳述。下面貼出來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_())

運行后,即出現如圖操作窗口

 

 

#運行效果:

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

發現任務已經在計划中。點擊Remove,刷新任務計划。

成功移除任務,功能實現

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

#打包:

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

pyinstaller -w shut.py

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

 

至此,關於最近做的windows自動關機小軟件的經驗敘述完畢~

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM